MediaPlayer.isPlaying() IllegalStateException的一種情況

  /**
     * Checks whether the MediaPlayer is playing.
     *
     * @return true if currently playing, false otherwise
     * @throws IllegalStateException if the internal player engine has not been
     * initialized or has been released.
     */
    public native boolean isPlaying();

沒初始化或者已經釋放會throws IllegalStateException 

也就是

沒有設置資源mediaPlayer.setDataSource(url);

沒有mediaPlayer.prepare();

只是new了一個對象的話,調用isPlaying(),就會IllegalStateException ,所以還是要try catch一下即可

      try {

            if(mpMediaPlayer.isPlaying()){
                mpMediaPlayer.stop();
                mpMediaPlayer.release();
                mpMediaPlayer = MediaPlayer.create(this, R.raw.dd);
            }

            mpMediaPlayer.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }

下面貼一張生命週期流程圖(來自何俊林的《android 音視頻開發》)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章