设备锁定时停止 android 应用程序音频播放 [英] Stop android app audio playing when device locked

查看:26
本文介绍了设备锁定时停止 android 应用程序音频播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 webview 的 Android 应用程序的情况.当用户导航到 YouTube 视频时,它开始播放(带音频),然后用户使用设备的硬件开关锁定设备,它继续播放音频.

I have a situation where we have an Android app using a webview. When the user navigates to a YouTube video, it starts playing (with audio), and then the user locks the device using the device's hardware switch, it keeps playing the audio.

使用设备菜单将应用发送到后台或退出应用时不会发生这种情况.

This does not occur when the app is sent to the background using the device's menu or when the app is exited.

有谁知道为什么会发生这种情况以及如何阻止它?

Does anyone know why this occurs and how to stop it?

我刚刚在这里找到了一个类似的未答复帖子:应用启用后台播放视频、cordova 问题、google play 拒绝应用

I just found a similar, unanswered, post here: App enables background playing of videos, cordova issues, google play rejecting apps

推荐答案

我遇到了与 Google 拒绝该应用程序相同的问题,因为当您按下电源按钮关闭屏幕.

I had the same problem with Google rejecting the app because the the webview continues to play the audio from the youtube video when you pressed the power button to turn off the screen.

由于某种原因,按下电源按钮似乎不会触发 webview 的 onPause(),这就是音频继续播放的原因.虽然这对于在屏幕关闭时保持音乐播放很有用,但 Google 不喜欢 youtube 视频的这种行为.

For some reason it seems that pressing the power button doesn't trigger the webview's onPause(), which is why the audio continues to play. While this is useful for say to keep the music playing while the screen is off, Google doesn't like this behavior for youtube videos.

为了让 Google 满意,您可以像这样覆盖您的活动/片段的 onPause():

To make Google happy, you can override onPause() of your activity/fragment like this:

@Override
public void onPause() {
    super.onPause();
    myCustomWebView.onPause();
}

现在按下电源按钮也会触发 webview 的 onPause().然而,这又产生了另一个问题:屏幕关闭时您真正想要继续播放的音频呢?

Pressing the power button will now also trigger the webview's onPause(). This however creates another problem: what about the audio that you actually want to keep playing while the screen is off?

我想出的一个解决方法是覆盖 WebViewClient 的 onLoadResource(),它允许我检查资源是否来自 youtube,这决定了是否应该在时机成熟时调用 webview 的 onPause().像这样的:

One fix I came up with is to override the WebViewClient's onLoadResource(), which allows me to check whether the resource is from youtube, which determines whether the webview's onPause() should be called when the time comes. Something like this:

myCustomWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onLoadResource(WebView webview, String url) {
        super.onLoadResource(webview, url);
        if (url.contains("youtube.com")) mShouldPause = true;
    }

    //...... plus any other method you want to override

});

当然,这确实意味着任何在其 url 中包含youtube.com"的资源都会触发该标志,但对于我的需要,这就足够了.所以 onPause() 方法现在看起来像这样:

Of course, this does mean that any resource that has "youtube.com" in it's url will trigger the flag, but for my need this will suffice. So the onPause() method will now look something like this:

@Override
public void onPause() {
    super.onPause();
    // mShouldPause is just a private boolean
    if (mShouldPause) {
        myCustomWebView.onPause();
    }
    mShouldPause = false;
}

所以这是我想出的解决方案,没有使用反射来搞乱 webview 的内部工作.您可能希望创建一个通知,其中包含一个停止音频或将用户带回您的应用的按钮,这样用户就不会忘记哪个应用发出声音并认为手机闹鬼!!!

So this is the solution I came up with without messing around with the webview's internal workings using reflection. You may want to create a notification that has a button to stop the audio or brings the user back to your app so the user does't forget which app is making the sound and think the phone is haunted!!!

这篇关于设备锁定时停止 android 应用程序音频播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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