我正在使用Media Player应用程序,我正在使用Runnable Thread,如下所示:
private Runnable UpdateSongTime = new Runnable() { <br>
public void run() { <br>
try{ <br>
startTime = mediaPlayer.getCurrentPosition(); <br>
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime))) <br>
); <br>
seekbar.setProgress((int)startTime); <br>
myHandler.postDelayed(this, 100); <br>
} <br>
catch(Exception e){ <br>
e.printStackTrace(); <br>
} <br>
}<br>
}; <br>
In onDestroy() I have written the following code to stop the thread <br>
protected void onDestroy() { <br>
super.onDestroy();<br>
mediaPlayer.stop();<br>
mediaPlayer.release();<br>
myHandler.removeCallbacksAndMessages(UpdateSongTime);<br>
<br>
}<br>
但是在单击后退按钮时,语句
中的runnable方法会发生非法状态异常" startTime = mediaPlayer.getCurrentPosition(); ". <br>
任何人都可以帮助我如何在背压上停止运行 任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
您可以致电UpdateSongTime.interrupt();
答案 1 :(得分:0)
请将Runnable UpdateSongTime
作为全局变量
并将以下代码放在onFinish()或onDestroy()
中if(UpdateSongTime != null)
UpdateSongTime.interrupt();