我正在尝试使用媒体播放器播放 .wav 文件。实际上,到目前为止,我已经完成了如下所示。它正在正确播放更新进度,但问题媒体播放器并未完全搜索。媒体播放器在文件总持续时间之前停止提供进度并停止更新进度。我还实现了 OnComplete 侦听器也没有收到回调。这是我的代码,任何人都可以发现我失踪的地方。
public void playAudioFile() {
try {
mMediaPlayer.reset();
setMediaPlayerSource();
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
Graphbar.setProgress(mMediaPlayer.getDuration());
}
});
bPlay.setBackgroundResource(R.drawable.pause_selector);
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Playing");
updateProgressBar();
}
}).start();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateProgressBar() {
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
}
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
if(mMediaPlayer.isPlaying()){
long totalDuration = Graphbar.getMax();
long currentDuration =mMediaPlayer.getCurrentPosition();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
tvTimeLeft.setText(""+ Utilities.milliSecondsToTimer(currentDuration));
System.out.println(currentDuration+"/"+totalDuration);
Graphbar.setProgress((int)currentDuration);
mHandler.postDelayed(this, 1);
}
}
};
private void setMediaPlayerSource() {
String audioFile = getFilename();
try {
mMediaPlayer.setDataSource(audioFile);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}