在我的应用程序中,我在listview中显示我的音频文件,并且当listitem特定文件的点击必须在listbar单元格中使用seekbar播放时。但现在我想在播放结束时更新ui。 我使用了onCompletionListener,但在重放开始时会重复调用。 这是我的代码
private void startPlayBack(final MessagesAdapter.ViewHolder viewHolder){
try {
if(mMediaPlayer!=null){
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
}
mMediaPlayer.reset();
mMediaPlayer.setDataSource(viewHolder.mChatMessage.mMessages);
mMediaPlayer.prepareAsync();
} catch (IOException e) {
Log.i("audioplay", "IOException *************",e);
}catch (IllegalStateException e) {
Log.i("audioplay", "IllegalStateException *************",e);
}
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
viewHolder.mAudioPlayBtn.setVisibility(View.VISIBLE);
viewHolder.mAudioPlayBtn.setImageDrawable(mContext.getResources().getDrawable(R.drawable.audio_close));
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.i("Completion Listener", "audio Completed");
}
});
viewHolder.mAudioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mMediaPlayer != null && fromUser) {
mMediaPlayer.seekTo(progress * 1000);
mMediaPlayer.start();
}
}
});
seekUpdation(viewHolder);
}
这是获取的日志信息,
08-20 07:28:24.099 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.099 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.099 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.103 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.107 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:24.107 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:25.739 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.415 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.415 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.419 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:26.419 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:27.323 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
08-20 07:28:27.959 14873-14873/com.cybosol.sabanet I/Completion Listener﹕ audio Completed
答案 0 :(得分:15)
在MediaPlayers
中使用ViewPager
时出现类似问题。事实证明,MediaPlayers
遇到了错误,导致OnCompletionListener
被调用。来自OnErrorListener documentation:
<强>返回强>
如果方法处理错误,则为true;如果不是,则为false。返回false,或者根本没有OnErrorListener,将导致调用OnCompletionListener。
为OnErrorListener
实施MediaPlayers
,以防止拨打OnCompletionListener
。