【问题标题】:Ionic / Android 8+: Audio stops playing after about 5 mins when screen is lockedIonic / Android 8+:屏幕锁定约 5 分钟后音频停止播放
【发布时间】:2018-08-18 17:09:55
【问题描述】:

我的应用使用 cordova-plugin-media 插件播放音乐。 cordova-plugin-background-mode 正在处理后台任务。

此代码块存在于我的 app.component.ts 中:

platform.ready().then(() => {
    this.backgroundMode.setDefaults({
      title: 'MyMusicApp',
        text: 'Android 8 problem!',
        resume: true,
        hidden: true,
        silent: true
    });
}

当媒体播放器提供者组件接收到“播放”命令时,它会调用:

this.backgroundMode.enable();

在 Android 6 和 7 上,当屏幕锁定时,音频会在后台很好地播放。但是,在 Android 8 上,每次屏幕锁定(或超时)时,大约 5 分钟后,背景音频就会被切断。

据我了解,Android 8+ 正在使用新实现来处理保持活动状态或在后台使用资源的第三方应用。上面的解决方案是尝试解决这个截止问题,但它似乎没有帮助......

有人能提供帮助吗?如何在 Android 8 上最好地处理背景音频?

【问题讨论】:

  • 看看这里github.com/katzer/cordova-plugin-background-mode/issues。他们还报告了 Android O 上的 5 分钟崩溃问题。所以cordova-plugin-background-mode 是你的问题,而不是cordova-plugin-media
  • 它不适用于 android 8+ 设备。
  • 我也有同样的问题,Android 8+ 和 9+

标签: android ionic-framework audio background android-8.0-oreo


【解决方案1】:

我的应用也遇到了同样的问题。截至今天,根据我的经验,原来的 cordova-plugin-background-mode 不适用于目标 API 级别 26+(Android 8+)。很多人分叉该项目以进行自己的修复,其中一些很可能会起作用。

至于我,我选择使用另一个插件:cordova-plugin-foreground-service。它适用于 Android 8+,但确实有效。

【讨论】:

    【解决方案2】:

    我可以使用以下代码解决此问题:

        if (this.isCordova) {
            // play the file
                this.backgroundMode.enable();
                this.backgroundMode.setDefaults({ title: "title", text: 'title'});
                this.backgroundMode.on('activate').subscribe(() => {
                    this.backgroundMode.disableWebViewOptimizations();
                });
    
    
        }
    

    并将其添加到 config.xml:

        <config-file parent="./" target="app/src/main/AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
            <uses-permission android:name="android.permission.WAKE_LOCK" />
            <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
        </config-file>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2019-08-19
      • 1970-01-01
      相关资源
      最近更新 更多