即使在修改设置或使用 js 后,Android webview 也无法自动播放 youtube 视频

标签 android video webview youtube

我正在 Android N 上测试我的应用程序并尝试在我的 WebView 中加载 YouTube 页面并自动播放它们。这是我的 onCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new TestWebViewClient());
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);


    mWebView.loadUrl("https://www.youtube.com/watch?v=hzz_6dmv03I?autoplay=1");
    //mWebView.loadUrl("https://vimeo.com/117116735");
}

以上不会在加载页面时自动播放 youtube 或 vimeo 视频。我还尝试将以下内容添加到我的 TestWebViewClient

public class TestWebViewClient extends WebViewClient {
    public void onPageFinished(WebView view, String url) {
        view.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); 
    }
}

这实际上成功地自动播放了 vimeo 视频链接,但是当我在 youtube 视频链接上使用它时,我收到以下错误:

Uncaught TypeError: Cannot read property 'play' of undefined

对于 youtube 视频,我还尝试在查找其类名后模拟点击播放按钮,但这也不起作用:

public class TestWebViewClient extends WebViewClient {
    public void onPageFinished(WebView view, String url) { 
        view.loadUrl("javascript:(function() { 
            document.getElementsByClassName('ytp-play-button')[0].click();
        })"); 
    }
}

如果有不涉及使用 Youtube 数据 API 的解决方案,请告诉我。

最佳答案

我能够使用 IFRAME_API 找到解决此问题的方法。我现在正在使用这个(code 是视频 ID,例如“hzz_6dmv03I”):

 void loadYTVideoInWebView(String code) {
    String frameVideo = "<html><body style='margin:0px;padding:0px;'>\n" +
            "        <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>\n" +
            "                var player;\n" +
            "        function onYouTubeIframeAPIReady()\n" +
            "        {player=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}\n" +
            "        function onPlayerReady(event){player.playVideo();}\n" +
            "        </script>\n" +
            "        <iframe id='playerId' type='text/html' width='400' height='360'\n" +
            "        src='https://www.youtube.com/embed/"+code+"?enablejsapi=1&autoplay=1' frameborder='0'>\n" +
            "        </body></html>";
    mWebView.loadDataWithBaseURL("http://www.youtube.com", frameVideo, "text/html", "utf-8", null);

不需要更改 TestWebViewClient,但有必要设置:

mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

关于即使在修改设置或使用 js 后,Android webview 也无法自动播放 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41308758/

相关文章:

java - Button.setOnClickListener(这个);错误

android - shouldOverrideUrlLoading 在页面加载时不起作用/捕获链接点击

android - 如何在 android 中禁用为 webview 按下的后退按钮?

android - 从 Android WebView 中启动 map

java - android:onListItemClick 不起作用

android - 如何在Android中创建具有不同分段颜色的水平进度条

python - 如何进行时间码计算?

ios - 在ios中顺序播放图像

java - 如何修复无法播放此视频 android 表单 url

android - 在 android ics 中加载 url 时 webview 变为空白