I\'m trying to create something where a single audio file is played and can be paused and manipulated with a progress/seek bar. I want to have an image taking up most of the
I think this can be work for you.
Create one thread that will check song position and will move seekbar like this,
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if(mediaPlayer.isplaying){
int mediaPos_new = mediaPlayer.getCurrentPosition();
int mediaMax_new = mediaPlayer.getDuration();
seekBar.setMax(mediaMax_new);
seekBar.setProgress(mediaPos_new);
handler.postDelayed(this, 100); //Looping the thread after 0.1 second
// seconds
}
}
};
And call that thread like this,
mediaPos = mediaPlayer.getCurrentPosition();
mediaMax = mediaPlayer.getDuration();
seekBar.setMax(mediaMax); // Set the Maximum range of the
seekBar.setProgress(mediaPos);// set current progress to song's
handler.removeCallbacks(moveSeekBarThread);
handler.postDelayed(moveSeekBarThread, 100);
public class DetailMusic extends Activity implements View.OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
TextView text_shown;
Handler seekHandler = new Handler();
Bundle bundle = new Bundle();
String Musicnames;
int mediaMax;
int mediaPos = 0;
MediaPlayer mp = new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailmusic);
bundle = getIntent().getExtras();
if (bundle != null) {
Musicnames = bundle.getString("musicnbames");
System.out.println("++++++++++++++++++++++++++++++++" + Musicnames);
Log.e("musicfoldername", Musicnames);
}
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
text_shown = (TextView) findViewById(R.id.text_shown);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
//seekUpdation();
String PATH_TO_FILE = Musicnames;
// MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(PATH_TO_FILE);
mp.prepare();
} catch (IOException e) {
}
mp.start();
seek_bar.setMax(mp.getDuration());
try {
mp.setDataSource(Musicnames);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaPos = mp.getCurrentPosition();
mediaMax = mp.getDuration();
seek_bar.setMax(mediaMax); // Set the Maximum range of the
seek_bar.setProgress(mediaPos);// set current progress to song's
seekHandler.removeCallbacks(moveSeekBarThread);
seekHandler.postDelayed(moveSeekBarThread, 100); //cal the thread after 100 milliseconds
}
private Runnable moveSeekBarThread = new Runnable() {
public void run() {
if (mp.isPlaying()) {
int mediaPos_new = mp.getCurrentPosition();
int mediaMax_new = mp.getDuration();
seek_bar.setMax(mediaMax_new);
seek_bar.setProgress(mediaPos_new);
seekHandler.postDelayed(this, 100); //Looping the thread after 0.1 second
}
}
};
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
mp.reset();
// mp.start(); text_shown.setText("Playing...");
break;
case R.id.pause_button:
mp.pause();
text_shown.setText("Paused...");
}
}
}