我再次向你提出一个我已经提出的问题。我有一个扩展类服务并实现类 Runnable 用于音频播放的类,我正在使用进度条来显示音乐再现的进度。在run方法中添加更新进度条值的代码,上面还可以,但是当我播放一首歌曲并没有完成上一首歌曲时跳转到另一首歌曲时,之前创建的线程没有被破坏,帮助我请?
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);
}
}
}