0
再次請您提出我已提交的問題。 我有一個類擴展了類服務並實現了用於音頻播放的類Runnable,我正在使用一個進度條來顯示音樂再現的進度。在run方法中添加代碼來更新進度條的值,在那裏沒問題,但是當我播放一首歌曲並跳到另一首而沒有完成前一個時,先前創建的線程不會被破壞,幫助我請?「Destroy」進度條中的線程
public class PlaySongService extends Service implements MediaPlayer.OnCompletionListener, Runnable {
public static MediaPlayer mediaPlayer = new MediaPlayer();
private ProgressBar pbSong;
private final String TAG = "PlaySongService";
@Override
public IBinder onBind(Intent intent) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void onCreate() {
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.reset();
pbSong = new WeakReference<ProgressBar>(Main.pbSong);
super.onCreate();
}
....... /* Selected song in listview*/
private void playSong(int position) {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(my_file_selected_in_listview);
mediaPlayer.prepare();
mediaPlayer.start();
pbSong.get().setProgress(0);
pbSong.get().setMax(mediaPlayer.getDuration());
new Thread(this).start();
} catch (Exception e) {
Log.e(TAG,e.getMessage());
}
}
@Override
public void run() {
int currentPosition= 0;
int total = mediaPlayer.getDuration();
while (mediaPlayer!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mediaPlayer.getCurrentPosition();
Log.i(TAG,Thread.currentThread().toString());
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
pbSong.get().setProgress(currentPosition);
}
}
}
嘗試檢查!正在嘗試關閉進度 – JRowan