Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mediaplayer setOnCompletionListener is calling repeatedly

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
like image 726
Nidheesh Avatar asked Dec 02 '22 14:12

Nidheesh


1 Answers

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.

like image 110
Seanalair Avatar answered Dec 11 '22 03:12

Seanalair