在我的应用程序中,我在listview中显示我的音频文件,当单击listitem时,必须在listview单元格中用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
最佳答案
我在aMediaPlayers
中使用ViewPager
时也遇到了类似的问题。原来MediaPlayers
遇到了一个错误,导致调用OnCompletionListener
。从OnErrorListener documentation开始:
退换商品
如果方法处理了错误,则为true;如果方法没有处理错误,则为false。返回false或根本没有onerrorlistener,将导致调用onCompletionListener。
为您的OnErrorListener
实现MediaPlayers
,以防止对OnCompletionListener
的外部调用。