WebView media content stops when the screen is locked ( activity's onStop is called)

瘦欲@ 提交于 2019-12-11 02:39:30

问题


I have an App that plays a video inside a WebView on my MainActivity. The problem I am having is that the video is pausing whenever I lock the screen (Activity's onStop is called, that is when the video in the WebView is paused).

But this behavior only occurs on Apis >= Lollipop(5.0).

I searched everywhere for a solution, but I could not find anything. I just want the video to keep playing even when the user is on LockScreen.


回答1:


If your WebView is pausing because of the onStop, you can override onWindowVisibilityChanged from WebView:

public class MyWebView extends WebView {

    public MyWebView(Context context) {
        super(context);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        if (visibility != View.GONE) super.onWindowVisibilityChanged(visibility);
    }
}


来源:https://stackoverflow.com/questions/47812876/webview-media-content-stops-when-the-screen-is-locked-activitys-onstop-is-cal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!