Android MediaPlayer seekbar goes to end after onDestroy();

36 Views Asked by At

So, I have this code for the MediaPlayer:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meditatie1);
    getSupportActionBar().setTitle("Mindfulness");
    ColorDrawable colorDrawable
            = new ColorDrawable(Color.parseColor("#006600"));
    getSupportActionBar().setBackgroundDrawable(colorDrawable);

        play_btn=findViewById(R.id.playButton);
        back_btn=findViewById(R.id.rewindButton);
        forward_btn=findViewById(R.id.forwardButton);
        stop_btn=findViewById(R.id.pauseButton);
        time_txt=findViewById(R.id.textView2);

        seekBar=findViewById(R.id.seekBar);

        mediaPlayer= MediaPlayer.create(this, R.raw.mindfulness_pentru_incepatori );

        seekBar.setClickable(false);
        // adding functionalities for the buttons

        play_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PlayMusic();
            }

            private void PlayMusic() {
                mediaPlayer.start();
                finalTime=mediaPlayer.getDuration();
                startTime=mediaPlayer.getCurrentPosition();
                if(oneTimeOnly==0)
                {
                    seekBar.setMax((int) finalTime);
                    oneTimeOnly=1;
                }
                time_txt.setText(String.format(
                        "%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
                        TimeUnit.MILLISECONDS.toSeconds((long) finalTime)-
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)finalTime))
                ));
                seekBar.setProgress((int) startTime);
                handler.postDelayed(UpdateSongTime, 100);
            }
            //creating runnable
            private Runnable UpdateSongTime=new Runnable() {
                @Override
                public void run() {
                    startTime=mediaPlayer.getCurrentPosition();
                    time_txt.setText(String.format("%d min, %d sec",
                            TimeUnit.MILLISECONDS.toMinutes((long) startTime),
                            TimeUnit.MILLISECONDS.toSeconds((long) startTime)
                                    -TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime))));

                    seekBar.setProgress((int) startTime);
                    handler.postDelayed(this, 100);

                }
            };
        });

        stop_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mediaPlayer.pause();
            }
        });
        forward_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int temp=(int) startTime;
                if((temp+forwardTime) <= finalTime)
                {
                    startTime=startTime+forwardTime;
                    mediaPlayer.seekTo((int ) startTime);

                } else {
                    Toast.makeText(Meditatie1.this, "can't make jump", Toast.LENGTH_SHORT).show();
                }
            }
        });
        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int temp= (int) startTime;
                if((temp-backTime)>0)
                {
                    startTime=startTime-backTime;
                    mediaPlayer.seekTo((int) startTime);
                } else {
                    Toast.makeText(Meditatie1.this, "Can't go back", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

My first problem was that, when I was pressing the back button from my phone the media was still playing. So I added this :

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mediaPlayer != null) {
        mediaPlayer.stop();
    }
}

Now it's kinda working, because the song stops , but I have another problem, when I first enter in the app, and press play the seekbar it's ok, like this : enter image description here

after pressing the back button, and going in the activity again, and pressing on play the seekbar goes to the end, like this: enter image description here What I can do ?

1

There are 1 best solutions below

0
Caden Wen On

There might be a problem with the seekBar.setMax() method. I saw that you used an if statement before setting the max value of the seekBar, but I don't understand how the oneTimeOnly property works. This might be causing issues when you come back to the activity for the second time and the seekBar max value is not set correctly.