gpt4 book ai didi

java - 在我的Androidstudio音乐播放器中暂停按钮问题

转载 作者:行者123 更新时间:2023-12-02 09:39:07 25 4
gpt4 key购买 nike

当我按下暂停按钮,然后按下播放按钮时,歌曲将从暂停位置播放。但是当歌曲结束然后按下播放按钮时,歌曲会从暂停位置播放,而不是从开头播放。

将我的代码粘贴到此处

 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;
}

最佳答案

看看 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();
}
}

关于java - 在我的Androidstudio音乐播放器中暂停按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com