Android – mediaplayer setoncompletionlistener repeated calls
•
Android
In my application, I display my audio files in listview, and when the click of a specific file of listitem must be played using seekbar in the listbar cell. But now I want to update the UI at the end of playback. I use oncompletionlistener, but it will be called repeatedly at the beginning of playback. This is my code
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);
}
This is the log information obtained,
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
resolvent:
A similar problem was encountered when using mediaplayers in viewpager. It turned out that mediaplayers encountered an error that caused oncompletionlistener to be called. From onerrorlistener documentation:
Implement an onerrorlistener for mediaplayers to prevent irrelevant calls to oncompletionlistener
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码