简体   繁体   English

科尔多瓦:暂停赛事没有开火

[英]Cordova: Pause event not firing

For some reason the pause event is not firing when it should. 由于某种原因,暂停事件不会在应该触发时触发。

If I press the home button on my phone, this is where the pause event should be fired, which should pause the music. 如果我按下手机上的主页按钮,则应该触发暂停事件,这应该暂停音乐。 But instead it keeps on playing it. 但相反,它继续播放它。

But when I open my app again it is suddenly paused and then resumed. 但是,当我再次打开我的应用程序时,它突然暂停,然后恢复。 So it fires both events when I return to the app. 因此,当我返回应用程序时,它会触发这两个事件。

Why is onPause not firing when I leave the app? 当我离开应用程序时,为什么onPause不会触发?

I have the following code: 我有以下代码:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("resume", onResume, false);
    document.addEventListener("pause", onPause, false);
}

function onPause() {
    PauseMusic();
    alert('paused');
}

function onResume() {
    ResumeMusic();
    alert('resumed');
}

I think I had your exact same problem. 我想我有同样的问题。 I had set the Phonegap config.xml variable KeepRunning to false. 我已将Phonegap config.xml变量KeepRunning设置为false。 I set to true and now it gets the pause event when it is supposed to. 我设置为true,现在它应该在它应该的时候获得暂停事件。 Here is the config.xml line I am talking about: 这是我正在讨论的config.xml行:

<preference name="KeepRunning" value="true"/>

I suppose it makes sense that if the app is not running then the pause event code won't get run until the app runs again. 我认为,如果应用程序没有运行,那么暂停事件代码将无法运行,直到应用程序再次运行。 I guess KeepRunning has higher priority than listening to pause event. 我猜KeepRunning的优先级高于监听暂停事件。 It would be interesting to get a proper explanation as to why this is exactly at a lower level. 获得一个正确的解释,为什么这是完全在较低的水平将是有趣的。

Are you testing on iOS? 你在iOS上测试吗? I found this to be "normal" behaviour on iOS, as also the Phonegap documentation points out : 我发现这在iOS上是“正常”行为,Phonegap文档也指出

In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). 在暂停处理程序中,对Cordova API或通过Objective-C的本机插件的任何调用都不起作用,以及任何交互式调用,例如警报或console.log()。 They are only processed when the app resumes, on the next run loop. 它们仅在应用程序恢复时在下一个运行循环中处理。

So according to the doc you will never see the alert('paused) when closing the app. 因此,根据文档,您在关闭应用程序时将永远不会看到alert('paused)

However, my observation is that the code is executed on pause of the app, only the console prints are delayed until the next app restart. 但是,我的观察是代码是在应用程序暂停时执行的,只有控制台打印才会延迟,直到下一个应用程序重新启动。

If your PauseMusic() call is not executed, you should probably post more code and see, if there's an issue there. 如果你的PauseMusic()调用没有被执行,你可能应该发布更多的代码,看看是否存在问题。

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

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