简体   繁体   English

Android MediaPlayer暂停

[英]Android MediaPlayer pause

The problem I am having at the moment is that my pause is not always working. 我目前遇到的问题是暂停并不总是有效。 What I have is an MediaPlayer in main activity that is operated via ActionBarSherlock and onClick listeners. 我的主要活动是通过ActionBarSherlock和onClick侦听器操作的MediaPlayer。 MediaPlayer is using ArrayList with URLs of MP3 files(some of them 1sec long). MediaPlayer正在将ArrayList与MP3文件的URL(某些文件的长度为1秒)一起使用。

Pause code: 暂停代码:

       if (player.isPlaying()) {
            if (player != null) {
                player.pause();
                swapPlayIcon(1);
                isPaused = true;
                pauseMenuButt.setVisible(false);
                playMenuButt.setVisible(true);
            }
        }

swapPlayIcon(int) handles only visibility and drawable swaps. swapPlayIcon(int)仅处理可见性和可绘制交换。

Start code: 起始码:

    Iterator<Uri> iterUri = tracks.iterator();
    while (iterUri.hasNext()) {
        Uri tmpUri = iterUri.next();
        try {
            player.reset();
            player.setDataSource(String.valueOf(tmpUri));
            player.prepare();
            player.start();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

EDIT 编辑

After more testing I have found out that the problem appear in "in between" state. 经过更多测试后,我发现问题以“介于”状态出现。

What I mean is that when my MP3 file is 2sec long and I click pause its not stopping becouse it just have ended reading one file and now moved on to the next one. 我的意思是,当我的MP3文件长2秒并且我单击“暂停”时,它没有停止,因为它刚读完一个文件,现在移到下一个文件了。

I have added: 我已经添加了:

      } else {
          pauseLocked = true;
      }

to pause if statment and it does not land in it at all while testing. 在测试时暂停并在声明中完全没有出现。 So im not sure about the "in between" problem that I have found out previously. 因此,我不确定之前发现的“中间”问题。

Your pause button is not working because there's no way for its message to ever reach the MediaPlayer until you've started the last URL. 您的暂停按钮不起作用,因为在您启动最后一个URL之前,其消息永远无法到达MediaPlayer。 You are entirely blocking the thread your MediaPlayer is running on (I'm assuming it's the UI thread since the MediaPlayer is in your main activity). 您完全阻塞了MediaPlayer所运行的线程(由于MediaPlayer在您的主要活动中,所以我假设它是UI线程)。 If you're creating your MediaPlayer on the same thread as your UI, you should use the asynchronous version of prepare: prepareAsync. 如果要在与UI相同的线程上创建MediaPlayer,则应使用prepare的异步版本:prepareAsync。 You need to respond once you receive the onMediaPrepared callback and then start the media. 收到onMediaPrepared回调后,您需要做出响应,然后启动媒体。 Once that happens you must do nothing and wait for the media to finish, only then should you load another url. 一旦发生这种情况,您就无需执行任何操作并等待媒体播放完毕,然后再加载另一个URL。

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

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