android - 如何在 Android 中重置媒体播放器的 SeekBar

标签 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一起工作得很好, 但是当我通过点击按钮再次播放时,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);

关于android - 如何在 Android 中重置媒体播放器的 SeekBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55495364/

相关文章:

安卓 : Changing color of an image using seekbar

javascript - 最初搜索视频后无法快退或前进?

java - 如何避免在 AsyncTask 中传递过多的输入参数?

android - Activity 导航逻辑

android - Android Oreo 上未添加地理围栏

Android: TextView 数组

java - 当 SeekBar 传递值时更改 TextView 颜色

android - 如何让 surfaceview 在 Android 中不可见?

android - 单击 Spinner 项目后,SeekBar 更改主题

Android:在 Canvas 上绘制 View