Как сбросить панель поиска в Android для медиаплеера

#android #seekbar

#Android #панель поиска

Вопрос:

Я работаю над приложением для чата с аудиосообщением с использованием медиаплеера.

Вот мой код:

 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
    }

}
  

Код написан внутри класса адаптера.

В первый раз, когда я проигрываю звук, он отлично работает с панелью поиска, но когда я проигрываю его снова, нажимая на кнопку, панель поиска колеблется из-за 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
  

Комментарии:

1. удалите hanlder после завершения работы медиаплеера

2. @Naveen можете ли вы подсказать мне, как я мог бы ее удалить. я удалил mHandler.postDelayed (this, 1000); но затем панель поиска не работала

3. @AbhishekChudekar, удалите этот обработчик с помощью их обратного вызова yourHandlerObject.removeCallbacks(yourRunnableObject);

4. @RakeshKumar я сделал это, но та же проблема.

5. Вы также можете использовать audioSeekBar.setProgress(0);

Ответ №1:

добавление return; всякий раз, когда выполняется ваше условие, может решить эту проблему

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

     return;
}
  

И затем

 mHandler.removeCallbacks(this);