首页 文章

无法使用Codename One停止iOS上的音频播放

提问于
浏览
1

我的Codename One应用程序在用户点击屏幕时在后台播放音频 . 我使用的音频是一个mp3 . 以下是我使用媒体播放的方式:

public static void playSound(boolean stop) {

 sound.reset(); // The input stream needs to go back to the beginning
 Media myClip = MediaManager.createMedia(sound, "audio/mp3", () -> {
                // If there is no order to stop playback, we keep playing when it has completed (looping)
                playSound(false);
            });

            if (!stop) {
                myClip.play();
            } else {
                myClip.cleanup();
            }
}

因此,当用户点击屏幕组件更改时,我将true传递给 playSound 方法 . 在Android上,当前播放不会停止在带有iPhone 4的iOS上 .

请注意,当应用程序最小化(按下中央按钮)时,播放停止(即使我没有在 Media 上调用 cleanup() ,我在Android上执行此操作以在应用程序最小化时停止播放) .

如何在iPhone上停止播放?

任何帮助,赞赏,

1 回答

  • 1

    @Shai指出了正确的方向,所以这里是最终使用的代码:

    Media myClip = null;
    
    public static void playSound(boolean stop) {
    
     sound.reset(); // The input stream needs to go back to the beginning
    
    
    
      /**
      * If the media is playing we don't create it
      * otherwise we would have several media in the wild 
      * that could not be stopped
      */
     if (myClip == null || !myClip.isPlaying()) {
    
          myClip = MediaManager.createMedia(sound, "audio/mp3", () -> {
                    // If there is no order to stop playback, we keep playing when it has completed (looping)
                    playSound(false);
                });
         }
    
                if (!stop) {
                    myClip.play();
                } else {
                    myClip.cleanup();
                }
    }
    

相关问题