在我的Androidstudio音乐播放器暂停按钮问题
In my Androidstudio music player pause button problem
当我按下暂停按钮然后按下播放按钮时,歌曲从暂停位置开始播放。但是当歌曲结束然后按下播放按钮时,歌曲从暂停位置开始播放,而不是从开始播放。
在此处粘贴我的代码
public void play(View view){
if(mediaPlayer==null) {
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
}
else if (!mediaPlayer.isPlaying()){
int nowMusicPosition = 0;
if (nowMusicPosition == mediaPlayer.getDuration()){
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
}
else {
mediaPlayer.seekTo(pauseCurrentPosition);
mediaPlayer.start();
nowMusicPosition = mediaPlayer.getCurrentPosition();
}
}
}
public void pause(View view){
if(mediaPlayer!=null) {
mediaPlayer.pause();
pauseCurrentPosition = mediaPlayer.getCurrentPosition();
}
}
public void stop(View view){
if (mediaPlayer!=null)
mediaPlayer.stop();
mediaPlayer = null;
}
这个条件有问题。
if (nowMusicPosition == mediaPlayer.getCurrentPosition()){
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
}
这不是在执行。重新检查一下。
即使您不需要重新创建媒体播放器实例,也只需使用此
mediaPlayer.start();
完成后从头开始播放。
希望对您有所帮助。
看看 API 我会说你做的太复杂了。您不必记住位置,因为如果您暂停播放,MediaPlayer 应该会记住位置本身。
所以只需使用 play()、pause() 就可以了。不求().
您的代码存在问题:
else if (!mediaPlayer.isPlaying()){
// this is called nowMusicPosition, down there you've used pauseCurrentPosition
// this always gets 0, never changes
int nowMusicPosition = 0;
if (nowMusicPosition == mediaPlayer.getCurrentPosition()){
// now you're recreating the MediaPlayer. why?
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
} else {
// if video at its end gets stopped, the
// getCurrentPosition() delivers the last
// position in the video and you get down
// here and you're seeking to the paused
// position again
mediaPlayer.seekTo(pauseCurrentPosition);
mediaPlayer.start();
// now you're setting nowMusicPosition, but it gets 0 again if pressing play again, see above
nowMusicPosition = mediaPlayer.getCurrentPosition();
}
}
当我按下暂停按钮然后按下播放按钮时,歌曲从暂停位置开始播放。但是当歌曲结束然后按下播放按钮时,歌曲从暂停位置开始播放,而不是从开始播放。
在此处粘贴我的代码
public void play(View view){
if(mediaPlayer==null) {
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
}
else if (!mediaPlayer.isPlaying()){
int nowMusicPosition = 0;
if (nowMusicPosition == mediaPlayer.getDuration()){
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
}
else {
mediaPlayer.seekTo(pauseCurrentPosition);
mediaPlayer.start();
nowMusicPosition = mediaPlayer.getCurrentPosition();
}
}
}
public void pause(View view){
if(mediaPlayer!=null) {
mediaPlayer.pause();
pauseCurrentPosition = mediaPlayer.getCurrentPosition();
}
}
public void stop(View view){
if (mediaPlayer!=null)
mediaPlayer.stop();
mediaPlayer = null;
}
这个条件有问题。
if (nowMusicPosition == mediaPlayer.getCurrentPosition()){
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
}
这不是在执行。重新检查一下。 即使您不需要重新创建媒体播放器实例,也只需使用此
mediaPlayer.start();
完成后从头开始播放。
希望对您有所帮助。
看看 API 我会说你做的太复杂了。您不必记住位置,因为如果您暂停播放,MediaPlayer 应该会记住位置本身。
所以只需使用 play()、pause() 就可以了。不求().
您的代码存在问题:
else if (!mediaPlayer.isPlaying()){
// this is called nowMusicPosition, down there you've used pauseCurrentPosition
// this always gets 0, never changes
int nowMusicPosition = 0;
if (nowMusicPosition == mediaPlayer.getCurrentPosition()){
// now you're recreating the MediaPlayer. why?
mediaPlayer = MediaPlayer.create(this, R.raw.ennasona);
mediaPlayer.start();
} else {
// if video at its end gets stopped, the
// getCurrentPosition() delivers the last
// position in the video and you get down
// here and you're seeking to the paused
// position again
mediaPlayer.seekTo(pauseCurrentPosition);
mediaPlayer.start();
// now you're setting nowMusicPosition, but it gets 0 again if pressing play again, see above
nowMusicPosition = mediaPlayer.getCurrentPosition();
}
}