/**
* 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 音視頻開發》)