Search code examples
javaandroidandroid-studioandroid-mediaplayer

Android MediaPlayer seekBar not working correctly


Currently, I'm developing a media player and I need seekBar to work together, at the moment, it's everything okay! but, when I touch at determinated position of the seekbar, the mediaplayer must follow and set the song to this determinated position, but the song and seekBar back to the start and I don't know why it's happening.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private Handler mHandler = new Handler();

    public TextView song_detail;
    public TextView time1;
    public TextView time2;
    private String player_status = "playing";
    private ImageButton player_img;

    public static SeekBar seekBar;

    private static MediaPlayer mediaPlayer;

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        song_detail = findViewById(R.id.song_detail);
        song_detail.setVisibility(View.GONE);

        time1 = findViewById(R.id.time_1);
        time2 = findViewById(R.id.time_2);

        seekBar = findViewById(R.id.seekBar);

        final Runnable mRunnable = new Runnable() {
            @Override
            public void run() {
                if(mediaPlayer != null){
                    int CurrentPosition = mediaPlayer.getCurrentPosition();
                    String m_1;
                    String s_1;
                    seekBar.setProgress(CurrentPosition/1000);
                    final int minutes_1 = (CurrentPosition/1000)/60;
                    final int seconds_1 = ((CurrentPosition/1000)%60);

                    if (minutes_1 < 10) {
                        m_1 = "0" + minutes_1;
                    } else {
                        m_1 = "" + minutes_1;
                    }

                    if (seconds_1 < 10) {
                        s_1 = "0" + seconds_1;
                    } else {
                        s_1 = "" + seconds_1;
                    }
                    time1.setText(m_1 + ":" + s_1);


                    int Duration = mediaPlayer.getDuration();
                    String m_2;
                    String s_2;
                    final int minutes_2 = (Duration/1000)/60;
                    final int seconds_2 = ((Duration/1000)%60);

                    if (minutes_2 < 10) {
                        m_2 = "0" + minutes_2;
                    } else {
                        m_2 = "" + minutes_2;
                    }

                    if (seconds_2 < 10) {
                        s_2 = "0" + seconds_2;
                    } else {
                        s_2 = "" + seconds_2;
                    }

                    time2.setText(m_2 + ":" + s_2);

                }
                mHandler.postDelayed(this, 1000);
            }
        };
        mRunnable.run();


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mediaPlayer != null && fromUser){
                    Toast.makeText(getApplicationContext(), String.valueOf(progress), Toast.LENGTH_LONG).show();
                    mediaPlayer.seekTo(progress);
                    seekBar.setProgress(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

    public void initAudio(final Context context, final String url) {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(context, Uri.parse(url));
        }
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //Toast.makeText(context, "TEST", Toast.LENGTH_LONG).show();
                killMediaPlayer();
            }
        });
        seekBar.setMax(mediaPlayer.getDuration()/1000);
        mediaPlayer.start();
    }

    public void killMediaPlayer() {
        if (mediaPlayer != null) {
            try {
                mediaPlayer.reset();
                mediaPlayer.release();
                mediaPlayer = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void pauseAudio() {
        if (!(mediaPlayer == null)) {
            mediaPlayer.pause();
        }
    }
    public static void startAudio() {
        if (!(mediaPlayer == null)) {
            mediaPlayer.start();
        }
    }
}

This is the part of code that I'm using to work with mediaPlayer and seekBar

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(mediaPlayer != null && fromUser){
                    Toast.makeText(getApplicationContext(), String.valueOf(progress), Toast.LENGTH_LONG).show();
                    mediaPlayer.seekTo(progress);
                    seekBar.setProgress(progress);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) { }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
        });
    }

But as I have said, instead of mediaPlayer seek and continue the audio from the position that I've clicked, the audio simply back to the start. What I'm doing wrong?

Thank you.


Solution

  • The max value for progress is 100. The seekTo function takes time in milliseconds. This is why it seeks to the start (almost) of the audio.

    So instead of mediaPlayer.seekTo(progress);, you need to do:

    long newTime = (progress/100.0) * total_audio_duration_in_millisecond; 
    mediaPlayer.seekTo(newTime);