问题
I have in connection a seekbar and a button alone used for play / pause. the principle is functional, when i press the button, it starts to play and in a second time when i press it pauses mediaplayer (perfect). The problem is when I press again so that it continues reading, the reading starts again from the beginning instead of continuing from the place where it had been paused. Please direct me, here is my code :
PLAY_SONG
private void play_songs() {
mediaPlayer = new MediaPlayer();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.setLegacyStreamType(AudioManager.STREAM_MUSIC)
.build());
} else {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
try {
Log.d("VERIFIONS link ", "CHANGEONS MESSAGE" + link);
mediaPlayer.setDataSource(loc);
Log.d("APRES SETSOURCE ", "APRES SETSOURCE" );
mediaPlayer.prepare();
Log.d("APRES PREPARE ASYNC", "APRES PREPARE ASYNC" );
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
seekbar.setMax(mp.getDuration());
int tailleduseekbarre = mp.getDuration();
Log.d("DANS ON PREPARE", "DANS ON PREPARE" );
//mp.start();
Log.d("APRES MP START ", "APRES MP START" );
Toast.makeText(context, "en lecture", Toast.LENGTH_SHORT).show();
//Log.d(" TAG DURATION", " VRIFIONS LA DUREE " + mp.getDuration());
Log.d(" TAG tailleduseekbar", " VRIFIONS LA DUREE tailleduseekbar" + tailleduseekbarre);
}
});
mediaPlayer.start();
PlayCycle();
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {
mediaPlayer.seekTo(mediaPlayer.getCurrentPosition());
//seekBar.setProgress(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener (new MediaPlayer.OnCompletionListener () {
@Override public void onCompletion (MediaPlayer mp) {
mediaPlayer.seekTo(0);
}
});
} catch (IOException e) {
e.printStackTrace();
Log.d("Prepare failed.: status", " ALORS" + equals(e.getMessage()));
Log.d("Catch De play song", "message " + e.getMessage());
}
}
PLAY_CYCLE
private void PlayCycle() {
seekbar.setProgress(mediaPlayer.getCurrentPosition());
Log.d(" POSITION ", "MEDIAPLAYER POSITION" + mediaPlayer.getCurrentPosition());
CurrentTimer2.setText(millisecondToTimer(mediaPlayer.getCurrentPosition()));
if (mediaPlayer.isPlaying()) {
Log.d("Dans le run"," DANS LE RUN");
Runnable runnable = new Runnable() {
@Override
public void run() {
PlayCycle();
// This code will always run on the UI thread, therefore is safe to modify UI elements.
}
};
handler.postDelayed(runnable, 100);
}
}
THERE IS WHERE IS CALL PLAY_SONG METHOD
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//Download();
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
seekbar.setProgress(mediaPlayer.getCurrentPosition());
}else{
//seekbar.setProgress(mediaPlayer.getCurrentPosition());
play_songs();
Toast.makeText(context, "Handle pause", Toast.LENGTH_SHORT).show();
}
}
});
Thanks
来源:https://stackoverflow.com/questions/60900792/how-to-evolve-a-seekbar-from-the-place-where-media-player-has-been-paused