Android媒体播放器没有完全寻求进展
Posted
技术标签:
【中文标题】Android媒体播放器没有完全寻求进展【英文标题】:Android mediaplayer not progress seeking completely 【发布时间】:2013-04-25 06:08:34 【问题描述】:我正在尝试使用媒体播放器播放 .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();
【问题讨论】:
【参考方案1】:总时长使用 mediaplayer.getduration。 从可运行处理程序中删除不必要的代码,因为它会为你的搜索栏创建负载。因为每秒你都在计算一些东西并根据该计算更新布局。
我建议你使用 chronometer android 视图。
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);
//set this only for once , because for one song total duration is always constant.
updateProgressBar();
public void updateProgressBar()
Graphbar.setProgress(0);
Graphbar.setMax(mMediaPlayer.getDuration());
mHandler.postDelayed(mUpdateTimeTask, 0);
long totalDuration = mMediaPlayer.getDuration();
String TotalDur = Utilities.milliSecondsToTimer(totalDuration);
tvTimeRight.setText(TotalDur);
private Runnable mUpdateTimeTask = new Runnable()
public void run()
if(mMediaPlayer.isPlaying())
//long totalDuration = mMediaPlayer.getDuration();=> no need of calculating total time and updating it to seekbar at every second
long currentDuration =mMediaPlayer.getCurrentPosition();
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();
【讨论】:
以上是关于Android媒体播放器没有完全寻求进展的主要内容,如果未能解决你的问题,请参考以下文章
Android - 在按钮单击时取消静音 VideoView