如何在 Android 上的 webView 中显示 YouTube 视频

Posted

技术标签:

【中文标题】如何在 Android 上的 webView 中显示 YouTube 视频【英文标题】:How to show YouTube video in webView on Android 【发布时间】:2017-11-17 20:19:12 【问题描述】:

我想在我的应用程序中显示 YouTube 视频,为此我想使用 WebView。 使用WebView 和显示视频我写在下面的代码:

            WebSettings settings = content_newsWebView.getSettings();
            content_newsWebView.setWebChromeClient(new WebChromeClient());
            content_newsWebView.getSettings().setDomStorageEnabled(true);
            content_newsWebView.getSettings().setAppCacheEnabled(true);
            content_newsWebView.getSettings().setAppCachePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/cache");
            content_newsWebView.getSettings().setDatabaseEnabled(true);
            content_newsWebView.getSettings().setDatabasePath(getApplicationContext().getFilesDir().getAbsolutePath() + "/databases");
            settings.setPluginState(WebSettings.PluginState.ON);
            content_newsWebView.getSettings().setjavascriptEnabled(true);
            settings.setDefaultTextEncodingName("utf-8");
            content_newsWebView.loadData(response.body().getData().getDescription(), "text/html; charset=UTF-8", null);

运行应用程序时,显示视频但不响应! 给我看最大的视频!请看这张图片来理解我的意思:Image Link

我想在我的应用程序中显示响应式 Youtube 视频。

我使用这个 Json 从服务器获取视频链接:

> "<p><iframe frameborder=\"0\"
> src=\"https://www.youtube.com/embed/NcoBDgJ3ZBw\"
> style=\"width:100%;min-height: 400px;\"></iframe></p>\n\n<p>The Harry
> Potter star plays an Israeli backpacker in the upcoming thriller from
> <a
> href=\"http://example.com/Celebrities/276442/greg_mclean\"><strong>Greg McLean</strong></a> (<a
> href=\"http://example.com/Movies/94393/the_belko_experiment\"><strong>The
> Belko Experiment</strong></a>, <a
> href=\"http://example.com/Movies/1397/wolf_creek\"><strong>Wolf
> Creek</strong></a>).</p>\n\n<p><a
> href=\"http://example.com/Celebrities/10139/daniel_radcliffe\"><strong>Radcliffe</strong></a>,
> 27, is a young adventurer going on a dream trip into the Amazonian
> rain forest along with two friends and a mysterious guide named
> Karl.</p>\n\n<p>Abandoned by their guide among the dangerous jungle
> creatures, Yossi (<a
> href=\"http://example.com/Celebrities/10139/daniel_radcliffe\"><strong>Radcliffe</strong></a>)
> and his friends are left with little means to survive, ending up in
> psychological battles with themselves.</p>\n\n<p>Starring <a
> href=\"http://example.com/Celebrities/64592/alex_russell\"><strong>Alex Russell</strong></a>, <a
> href=\"http://example.com/Celebrities/53829/thomas_kretschmann\"><strong>Thomas
> Kretschmann</strong></a> and <strong>Yasmin Kassim</strong>, Jungle
> premiers at Melbourne International Film Festival on Aug. 3.</p>\n"

如何在我的应用程序中显示响应式 youtube 视频?

请帮帮我,我真的需要你们的帮助。

【问题讨论】:

Play Youtube HTML5 embedded Video in android WebView的可能重复 @KaranSharma,我试试这个,但对我不起作用 你提供的json格式错误 【参考方案1】:

尝试以下代码:更改字符串中视频的 URL。

String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/47yJ2XCRLZs\" frameborder=\"0\" allowfullscreen></iframe></body></html>";

    WebView displayYoutubeVideo = (WebView) findViewById(R.id.mWebView);
    displayYoutubeVideo . setWebViewClient ( New  WebViewClient () 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
            return false;
        
    );
    WebSettings webSettings = displayYoutubeVideo . getSettings ();
    webSettings.setJavaScriptEnabled(true);
    displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");

【讨论】:

谢谢我的朋友,你能用我上面的代码编辑你的代码吗?请我是业余爱好者。请 你可以独立使用这个代码,你可以根据你的需求在它上面进行修改

以上是关于如何在 Android 上的 webView 中显示 YouTube 视频的主要内容,如果未能解决你的问题,请参考以下文章

如何在nativescript vue中处理webview上的android后退按钮

如何从不在 Android 的 API 26 上的 WebView 获取 WebviewClient 实例?

如何在 Android 上的 WebView 中读取 Blob 中的数据?

取消在 Android 上的 WebView 中加载

如何在 Android/iOS 上的 webview/移动浏览器中最好地预缓存视频?

如何将html字符串传递给android上的webview