Home > Enterprise >  Media Player loading media again and again
Media Player loading media again and again

Time:10-01

I am making a chat application and I have implemented the feature for sending audio messages.But here I find one thing which I don't want it to happen.It is that whenever my adapter gets updated,The media player starts loading again. In this way there will be an issue for if someone is listening to an audio and the user at other end sends a message ,the media player stops and it loads again.Here is the code of my adapter.

final MediaPlayer mediaPlayer;
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            handler = new Handler();
            try {
                mediaPlayer.setOnCompletionListener(mediaPlayer1 -> {
                    mediaPlayer1.stop();
                    binding.audioSeekbar.setProgress(0);
                });
                if (mediaPlayer.isPlaying()){
                    mediaPlayer.stop();
                    mediaPlayer.release();
                }
                mediaPlayer.setDataSource(finalUrlToLoad[1]);
                mediaPlayer.setVolume(1f, 1f);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(mediaPlayer1 -> {
                    int totalDuration = mediaPlayer1.getDuration();
                    binding.totalDurationAudio.setText(createTimeLabel(totalDuration));
                    binding.loadingAudio.setVisibility(GONE);
                    binding.playPauseAudio.setVisibility(VISIBLE);
                });
            } catch (IOException e) {e.printStackTrace();}
            binding.playPauseAudio.setOnClickListener(view -> {
                if (mediaPlayer.isPlaying()){
                    handler.removeCallbacks(runnable);
                    mediaPlayer.pause();
                    binding.playPauseAudio.setImageResource(R.drawable.pause_to_play);
                    Drawable drawable = binding.playPauseAudio.getDrawable();
                    if( drawable instanceof AnimatedVectorDrawable) {
                        AnimatedVectorDrawable animation = (AnimatedVectorDrawable) drawable;
                        animation.start();
                    }
                }else {
                    mediaPlayer.seekTo(binding.audioSeekbar.getProgress());
                    mediaPlayer.start();
                    handler.post(runnable);
                    binding.playPauseAudio.setImageResource(R.drawable.play_to_pause);
                    Drawable drawable = binding.playPauseAudio.getDrawable();
                    if( drawable instanceof AnimatedVectorDrawable) {
                        AnimatedVectorDrawable animation = (AnimatedVectorDrawable) drawable;
                        animation.start();
                    }
                }
            });
            runnable = () -> {
                int totalTime = mediaPlayer.getDuration();
                binding.audioSeekbar.setMax(totalTime);
                int currentPosition = mediaPlayer.getCurrentPosition();
                binding.audioSeekbar.setProgress(currentPosition);
                binding.totalDurationAudio.setText(createTimeLabel(totalTime));
                Log.d("time", String.valueOf(currentPosition));
                handler.postDelayed(runnable,1000);
            };
            binding.audioSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                    if (b){
                        mediaPlayer.seekTo(i);
                        seekBar.setProgress(i);
                    }
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });
            mediaPlayer.setOnBufferingUpdateListener((mediaPlayer1, i) -> binding.audioSeekbar.setSecondaryProgress(i));

Here finalurltoload[1] is the url for the audio.

Now what do I need to do in order to prevent loading it again and again.

I will be really grateful to who answer this question.

Thanks

  • Related