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

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

问题描述

我有一个情况,我们有一个Android应用程序使用webview。当用户导航到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拒绝应用时遇到了同样的问题,因为当您按电源按钮关闭屏幕时,webview会继续播放youtube视频中的音频。

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

});

当然,这意味着任何在其网址中包含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;
}

所以这是我想出的解决方案,使用反射的工作。您可能想创建一个通知,其中有一个按钮停止音频或带回用户回到您的应用程序,所以用户不会忘记哪个应用程序发出声音,认为手机困扰了!!!

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天全站免登陆