其他分享
首页 > 其他分享> > android – Mediaplayer setOnCompletionListener重复调用

android – Mediaplayer setOnCompletionListener重复调用

作者:互联网

在我的应用程序中,我在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

解决方法:

在ViewPager中使用MediaPlayers时遇到了类似的问题.事实证明,MediaPlayers遇到了一个错误,导致调用OnCompletionListener.从OnErrorListener documentation

Returns

True if the method handled the error, false if it didn’t. Returning false, or not having an one rrorListener at all, will cause the OnCompletionListener to be called.

为MediaPlayers实现一个OnErrorListener,以防止对OnCompletionListener进行无关的调用.

标签:android,listener,media-player,android-seekbar
来源: https://codeday.me/bug/20190713/1453631.html