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

如何在Android中为媒体播放器重置SeekBar,android,seekbar,Android,Seekbar,我正在使用媒体播放器开发一个带有音频消息的聊天应用程序 这是我的密码: public void playAudio(ImageView play_pause_ImageView, ProgressBar downloadProgressBar, chat message, SeekBar audioSeekBar) { play_pause_ImageView.setVisibility(View.INVISIBLE); downloadProgressBar.setVisi

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

这是我的密码:

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
    }

}
代码编写在适配器类中

我第一次播放音频时,seekBar可以正常工作, 但当我点击按钮再次播放时,seekBar会因为runOnUiThread而波动

以下是生成的日志:

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

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

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

     return;
}
然后

mHandler.removeCallbacks(this);

卸下媒体播放器后的手柄completed@Naveen你能建议我怎么把它取下来吗。我移除了mHandler.postdayed(这个,1000);但那个么seek bar并没有这样做worked@AbhishekChudekar,通过回调函数[code>yourHandlerObject.removeCallbacks(yourRunnableObject)]删除该处理程序
@RakeshKumar我这样做了,但问题是一样的。你也可以使用
audioSeekBar.setProgress(0)