[英]Android MediaPlayer, SeekBar Exception when change activity
我目前正在開發具有音頻播放功能的android應用程序。 在play_pause活動中,我有一個搜索欄和一個控制音頻的按鈕。
public class PlayerActivity extend AppCompatActivity {
...
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerButton = (Button) findViewById(R.id.playButton);
playerButton.setOnClickListener(new Button.setOnClickListener() {
if (player != null) {
//load music to player
player.create(PlayerActivity.this, R.raw.music);
seekbar.setMax(player.getDuration());
mTimer = new Timer();
mTimerTask = new TimerTask() {
public void run() {
seekbar.setProgress(player.getCurrentPosition());;
}
};
mTimer.schedule(mTimerTask, 0, 10);
}
});
}
}
當我按下后退按鈕而不點擊“播放”按鈕時,我的應用程序可以成功更改活動。 但是,一旦單擊“播放”按鈕,我將無法更改活動。 下面是onDestroy()方法。
protected void onDestroy() {
super.onDestroy();
player.release();
}
Logcat的錯誤消息是
java.lang.IllegalStateException
at MediaPlayer.getCurrentPosition()
at PlayerActivity.run() //which is the mTimerTask
at java.util.Timer$TimerImpl.run()
有人知道如何解決嗎?
也可以通過致電取消您的計時器onDestroy活動
mTimer.cancle();
由於調用player.getCurrentPosition()引發異常。 釋放媒體播放器實例后,由於計時器線程停止,Activity銷毀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.