This method play the song number of times I clicked on the play button. it should not happen but its not working accordingly
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.chelseafc);
mediaPlayer = MediaPlayer.create(MainActivity.this, path);
length = mediaPlayer.getCurrentPosition();
if (length == 0) {
mediaPlayer.start();
// length = mediaPlayer.getCurrentPosition();
} else if (length != 0) {
mediaPlayer.pause();
mediaPlayer.seekTo(length);
mediaPlayer.start();
}
// mediaPlayer.start();
}
});
You do not seem to stop previous songs.
Assuming that this is the only place where mediaPlayer
is initialized, you could replace this line: mediaPlayer = MediaPlayer.create(MainActivity.this, path);
with the following:
if((mediaPlayer != null) && (mediaPlayer.isPlaying()))
mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(MainActivity.this, path);
You probably also need to close the current media player first before starting a new one.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.