简体   繁体   English

Android 媒体播放器在暂停后无法启动

[英]Android mediaplayer won't start after pause

I'm trying make a simple mediaplayer but the pause button doesn't work.我正在尝试制作一个简单的媒体播放器,但暂停按钮不起作用。 When I click on the pause button it stops but when I click on play again it starts at the start again.当我单击暂停按钮时,它会停止,但是当我再次单击播放时,它又会从头开始。

I don't know how to make one button where I can play/pause the button.我不知道如何制作一个可以播放/暂停按钮的按钮。

Code I currently have:我目前拥有的代码:

http://pastebin.com/wiDkzw5S http://pastebin.com/wiDkzw5S

Thanks already!已经谢谢了!

You would need to save current position of media player and restart it later from that position using seekTo .您需要保存媒体播放器的当前位置,然后使用seekTo从该位置重新启动它

Something like:就像是:

int currentPos = 0;
pause.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

        if (mediaPlayer.isPlaying()) {
            currentPos = mediaPlayer.getCurrentPosition();
            mediaPlayer.pause();
            //change image to play
        } else {
            mediaPlayer.seekTo(currentPos);
            mediaPlayer.start();
            //again revert image to pause
        }  
    }
});

Hope it helps.希望能帮助到你。

This is running test and running code, you may use this for pause and resume the media player.这是运行测试和运行代码,您可以使用它来暂停和恢复媒体播放器。

int length=0;  
Button pause = (Button) findViewById(R.id.pause);
 pause.setOnClickListener(new OnClickListener() {
 public void onClick(View v) {
 if (mediaPlayer.isPlaying()) {
         mediaPlayer.pause();
        length=mediaPlayer.getCurrentPosition();    
    } else {
        mediaPlayer.seekTo(length);
        mediaPlayer.start();
          }
       }
  });

I had a similar problem, but I couldn't get it to resume using the methods above.我有一个类似的问题,但我无法使用上述方法恢复它。

I discovered my pauseAllSounds() function was pausing all MediaPlayer instances in my sound pool even if they weren't already playing.我发现我的 pauseAllSounds() 函数会暂停我的声音池中的所有 MediaPlayer 实例,即使它们尚未播放。 When that happened, it caused an error in each instance which wasn't playing which prevented that instance from playing again later.发生这种情况时,它会在每个未播放的实例中导致错误,从而阻止该实例稍后再次播放。 I discovered this after some time only by happening across the console output for my running process searching for the cause.一段时间后,我只是通过在控制台输出中为我正在运行的进程搜索原因而发生的事情发现了这一点。 It showed line after line of errors, revealing I was trying to pause from an invalid state.它显示了一行又一行的错误,表明我正试图从无效状态暂停。

// Make sure you test isPlaying() before pausing
public void pause() {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
        position = mediaPlayer.getCurrentPosition();
    }
}

Once I added the test to only pause if it was already playing, everything worked.一旦我将测试添加为仅在它已经播放时暂停,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM