Iphone Can';t停止iOS上代号为1的音频播放
My Codename One应用程序的特点是,当用户点击屏幕时,可以在后台播放音频。我使用的音频是mp3。以下是我如何使用媒体播放:Iphone Can';t停止iOS上代号为1的音频播放,iphone,codenameone,Iphone,Codenameone,My 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", () -&
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上
请注意,当应用程序最小化(按下中间按钮)时,播放停止(即使我没有在媒体上调用cleanup()
,我在Android上这样做是为了在应用程序最小化时停止播放)
如何在iPhone上停止播放
感谢您的帮助,@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();
}
}
你不是每次都创建一个新媒体实例,只停止播放新媒体而不是播放新媒体吗?谢谢Shai为我指出了正确的方向。因此,我进一步声明了myClip,但这还不够。我必须createMedia
onlyif(myClip==null | | |!myClip.isplay())
来防止媒体在没有任何可能停止的情况下处于野生状态。我被它在Android上工作的事实分心了!