在Android的简历上触发了Phonegap/Cordova暂停事件;结果无法关闭音频 [英] Phonegap / Cordova pause event triggered on resume for android; can not turn off audio as a result

查看:86
本文介绍了在Android的简历上触发了Phonegap/Cordova暂停事件;结果无法关闭音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Phonegap应用程序,该应用程序可以使用Phonegap Media插件播放音频.当我按我的android设备(KitKat 4.4)上的主屏幕按钮或屏幕锁定按钮时,该应用程序会按应有的方式进入后台,但由于未调用暂停事件,因此音频没有关闭.只有当我恢复该应用程序时,暂停事件才会运行,并且我关闭音频的代码最终会运行.恢复事件也将在此之后运行.

I am developing a Phonegap app that plays audio using the Phonegap Media plugin. When I press the home button or screen lock button on my android device (KitKat 4.4), the app goes into the background as it should but the audio does not shut off because the pause event is not getting called. Only when I resume the app does the pause event run, and my code to turn off the audio finally runs. The resume event also runs right after this.

阅读有关pause事件的Phonegap文档,其中有关于iOS Quirk的注释:

Reading the Phonegap docs on the pause event, there is a note about iOS Quirks:

在暂停处理程序中,对通过Objective-C进行的Cordova API或本机插件的任何调用以及任何交互式调用(例如alert或console.log())均不起作用.仅在以下情况下进行处理该应用将在下一个运行循环中恢复运行."

"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(). They are only processed when the app resumes, on the next run loop."

这与我在Android设备上看到的非常相似,尽管我想知道是否是同一问题.我确实需要一个修复程序或解决方法,因为这是当用户暂停应用程序时无法关闭音频的绝妙方法.

This is very similar to what I see on my Android device, though I am wondering if it is the same issue. I really need a fix or workaround as this is a showstopper to not be able to turn off audio when a user pauses the app.

这是我添加暂停事件监听器的代码:

Here is my code to add the pause event listener:

document.addEventListener('deviceready', on_device_ready, false);
function on_device_ready()
{
    document.addEventListener('pause', on_pause, false);
    document.addEventListener('resume', on_resume, false);
}

更新:我想我已经解决了,尽管我对该解决方案不太满意.我已经将Phonegap config.xml变量KeepRunning设置为false.我设置为true,现在它应该在应该的时候获得pause事件.我觉得我应该能够停止我的应用程序在后台运行,并且还可以事先运行一些暂停代码.

UPDATE: I think I've solved it, though I'm not too happy about the solution. I had set the Phonegap config.xml variable KeepRunning to false. I set to true and now it gets the pause event when it is supposed to. I feel like I should be able to stop my app from running in the background and also run some pause code beforehand as well.

无论如何,这是我正在谈论的config.xml行:

Anyway, here is the config.xml line I am talking about:

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

推荐答案

在为自己的论文开发mp3播放器时,我在第一个android版本中遇到了相同的问题.
问题是我没有创建线程,所以stop函数不是优先执行的.

When I was developing an mp3 player for my thesis, I was encountering the same issue in the first android version.
The issue was that I was not creating threads and so the stop function was not prioritary to be executed.

我知道我的论文不在科尔多瓦,而是在阅读科尔多瓦的文档中指出

I know that my thesis was not in Cordova, but reading Cordova documentation it states

Threading. JavaScript in the WebView does not run on the UI thread. It runs on the 
WebCore thread

来自 Cordova文档

还要查看 Android文档,您是否考虑过实施一个处理适合Cordova插件的多线程和任务队列的类?

Looking also at Android Documentation, have you thought of implementing a class that handles multiple thread and task queue that fits with Cordova plugin ?

这篇关于在Android的简历上触发了Phonegap/Cordova暂停事件;结果无法关闭音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆