SeekBar and media player in android
To create a 'connection' between SeekBar
and MediaPlayer
you need first to get your current recording max duration and set it to your seek bar.
mSeekBar.setMax(mFileDuration/1000); // where mFileDuration is mMediaPlayer.getDuration();
After you initialise your MediaPlayer
and for example press play button, you should create handler and post runnable so you can update your SeekBar
(in the UI thread
itself) with the current position of your MediaPlayer
like this :
private Handler mHandler = new Handler();//Make sure you update Seekbar on UI threadMainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { if(mMediaPlayer != null){ int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000; mSeekBar.setProgress(mCurrentPosition); } mHandler.postDelayed(this, 1000); }});
and update that value every second.
If you need to update the MediaPlayer
's position while user drag your SeekBar
you should add OnSeekBarChangeListener
to your SeekBar
and do it there :
mSeekBar.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(mMediaPlayer != null && fromUser){ mMediaPlayer.seekTo(progress * 1000); } } });
And that should do the trick! : )
EDIT:One thing which I've noticed in your code, don't do :
public MainActivity() { mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); mFileName += "/audiorecordtest.3gp";}
make all initialisations in your onCreate();
, do not create constructors of your Activity
.
I've used this tutorial with success, it's really simple to understand:www.androidhive.info/2012/03/android-building-audio-player-tutorial/
This is the interesting part:
/** * Update timer on seekbar * */public void updateProgressBar() { mHandler.postDelayed(mUpdateTimeTask, 100);} /** * Background Runnable thread * */private Runnable mUpdateTimeTask = new Runnable() { public void run() { long totalDuration = mp.getDuration(); long currentDuration = mp.getCurrentPosition(); // Displaying Total Duration time songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration)); // Displaying time completed playing songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration)); // Updating progress bar int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration)); //Log.d("Progress", ""+progress); songProgressBar.setProgress(progress); // Running this thread after 100 milliseconds mHandler.postDelayed(this, 100); } };/** * * */@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {}/** * When user starts moving the progress handler * */@Overridepublic void onStartTrackingTouch(SeekBar seekBar) { // remove message Handler from updating progress bar mHandler.removeCallbacks(mUpdateTimeTask);}/** * When user stops moving the progress hanlder * */@Overridepublic void onStopTrackingTouch(SeekBar seekBar) { mHandler.removeCallbacks(mUpdateTimeTask); int totalDuration = mp.getDuration(); int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration); // forward or backward to certain seconds mp.seekTo(currentPosition); // update timer progress again updateProgressBar();}
After you initialize your MediaPlayer
and SeekBar
, you can do this :
Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { mSeekBar.setProgress(mMediaPlayer.getCurrentPosition()); } },0,1000);
This updates SeekBar
every second(1000ms)
And for updating MediaPlayer
, if user drag SeekBar
, you must add OnSeekBarChangeListener
to your SeekBar
:
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { mMediaPlayer.seekTo(i); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } });
HAPPY CODING!!!