In my application i am showing my audio files in listview, and when clicks on listitem particular file has to be played inside the listview cell with seekbar. But now i want to update the ui when playback finishes. I have used onCompletionListener, but it invokes repeatedly when playback starts. 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 got,
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
I had a similar problem when using MediaPlayers
in a ViewPager
. It turned out the MediaPlayers
were encountering an error, which was causing the OnCompletionListener
to be called. From the OnErrorListener documentation:
Returns
True if the method handled the error, false if it didn't. Returning false, or not having an OnErrorListener at all, will cause the OnCompletionListener to be called.
Implement an OnErrorListener
for your MediaPlayers
to prevent extraneous calls to OnCompletionListener
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With