如何在Android中为媒体播放器重置SeekBar

时间:2019-04-03 12:35:38

标签: android seekbar

我正在使用带有媒体播放器的音频消息的聊天应用程序。

这是我的代码:

public void playAudio(ImageView play_pause_ImageView, ProgressBar downloadProgressBar, chat message, SeekBar audioSeekBar) {


    play_pause_ImageView.setVisibility(View.INVISIBLE);
    downloadProgressBar.setVisibility(View.VISIBLE);
    downloadProgressBar.setVisibility(View.GONE);
    play_pause_ImageView.setVisibility(View.VISIBLE);
    play_pause_ImageView.setImageResource(R.drawable.ic_pause_black_24dp);

    MediaPlayer mediaPlayer = new MediaPlayer();
    try {

        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setDataSource(message.getAudioUrl());
        mediaPlayer.prepare();


        final Handler mHandler = new Handler();

        message.getChatActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mediaPlayer != null) {

                    Log.d("ddddddddddddddddd",mediaPlayer.getDuration() / 1000+"");
                    Log.d("lllllllllllllllll",mediaPlayer.getCurrentPosition() / 1000+"");

                    audioSeekBar.setMax(mediaPlayer.getDuration() / 1000);

                    audioSeekBar.setProgress(mediaPlayer.getCurrentPosition() / 1000);


                    audioSeekBar.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 (fromUser) {
                                Log.d("aaaaaaaaa", progress + "");
                                mediaPlayer.seekTo(progress);
                                seekBar.setMax(progress);
                            }*/

                        }
                    });
                }

                mHandler.postDelayed(this, 1000);

            }


        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                play_pause_ImageView.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                mediaPlayer.stop();


            }
        });

        mediaPlayer.start();


        Toast.makeText(mContext, "Playing Audio", Toast.LENGTH_LONG).show();
    } catch (Exception e) {

        // make something
    }

}

代码写在Adapter类中。

当我第一次播放音频时,它可以与seekBar配合使用, 但是当我通过单击按钮再次播放它时,由于runOnUiThread,seekBar会波动。

这是生成的日志:

2019-04-03 17:58:51.921 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:51.922 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 0
2019-04-03 17:58:51.935 28970-28970/in.demo.androidchatdemo I/Choreographer: Skipped 109 frames!  The application may be doing too much work on its main thread.
2019-04-03 17:58:52.023 28970-28990/in.demo.androidchatdemo D/EGL_emulation: eglMakeCurrent: 0xdd884ec0: ver 2 0 (tinfo 0xe57ba7b0)
2019-04-03 17:58:52.923 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:52.923 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 0
2019-04-03 17:58:53.925 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:53.926 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 1
2019-04-03 17:58:53.943 28970-28990/in.demo.androidchatdemo D/EGL_emulation: eglMakeCurrent: 0xdd884ec0: ver 2 0 (tinfo 0xe57ba7b0)
2019-04-03 17:58:54.928 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:54.929 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 2
2019-04-03 17:58:55.931 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:55.932 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 3
2019-04-03 17:58:56.934 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:56.935 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:58:57.937 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:57.938 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:58:58.939 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:58.940 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:58:59.942 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:58:59.942 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:00.944 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:00.944 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:01.946 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:01.946 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:02.948 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:02.949 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:03.950 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:03.950 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:04.952 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:04.952 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:05.954 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:05.954 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:06.955 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:06.955 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:07.402 28970-28970/in.demo.androidchatdemo W/MediaPlayer: Use of stream types is deprecated for operations other than volume control
2019-04-03 17:59:07.402 28970-28970/in.demo.androidchatdemo W/MediaPlayer: See the documentation of setAudioStreamType() for what to use instead with android.media.AudioAttributes to qualify your playback use case
2019-04-03 17:59:07.402 28970-28970/in.demo.androidchatdemo V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@ebd49d1): Cookies: null
2019-04-03 17:59:07.405 28970-29039/in.demo.androidchatdemo V/MediaHTTPService: makeHTTPConnection: CookieHandler (java.net.CookieManager@ca4d68b) exists.
2019-04-03 17:59:07.405 28970-29039/in.demo.androidchatdemo V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@ebd49d1): cookieHandler: java.net.CookieManager@ca4d68b Cookies: null
2019-04-03 17:59:09.039 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:09.039 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 0
2019-04-03 17:59:09.052 28970-28970/in.demo.androidchatdemo I/Choreographer: Skipped 99 frames!  The application may be doing too much work on its main thread.
2019-04-03 17:59:09.065 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:09.065 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:09.142 28970-28990/in.demo.androidchatdemo D/EGL_emulation: eglMakeCurrent: 0xdd884ec0: ver 2 0 (tinfo 0xe57ba7b0)
2019-04-03 17:59:10.042 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:10.043 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 0
2019-04-03 17:59:10.060 28970-28990/in.demo.androidchatdemo D/EGL_emulation: eglMakeCurrent: 0xdd884ec0: ver 2 0 (tinfo 0xe57ba7b0)
2019-04-03 17:59:10.068 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:10.068 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:11.045 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:11.045 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 1
2019-04-03 17:59:11.069 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:11.069 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:12.048 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:12.048 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 2
2019-04-03 17:59:12.071 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:12.071 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:13.050 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:13.050 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 3
2019-04-03 17:59:13.072 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:13.073 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:14.052 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:14.053 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:14.076 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:14.076 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:15.054 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:15.055 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:15.076 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:15.077 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:16.056 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:16.056 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4
2019-04-03 17:59:16.077 28970-28970/in.demo.androidchatdemo D/ddddddddddddddddd: 4
2019-04-03 17:59:16.078 28970-28970/in.demo.androidchatdemo D/lllllllllllllllll: 4

1 个答案:

答案 0 :(得分:2)

只要您的条件得到满足,添加return;可能会解决此问题

if (mediaPlayer.getDuration() / 1000 == mediaPlayer.getCurrentPosition() / 1000) {

     return;
}

然后

mHandler.removeCallbacks(this);