为什么Youtube视频没有在webview android中播放

时间:2017-07-24 05:51:54

标签: android webview youtube sdk html5-video

我在Android中有简单的webView,我想通过URL播放视频。 但它显示错误视频无法在webview中播放。 this error is coming while playing

这是我的源代码: -

WebView webView;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.livepage);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_bottom7);
    webView = (WebView)findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webSettings.setPluginState(WebSettings.PluginState.ON);
    webSettings.setJavaScriptEnabled(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE,null);
    webView.loadUrl("https://www.youtube.com/channel/UCLmI8_hVUqA251PzK7OSvQw");
    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new loadWebView());
}

private class loadWebView extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }
}

这是清单: -

<application
   ...
   android:supportsRtl="true"
   android:largeHeap="true"
   android:hardwareAccelerated="true">

提前致谢

4 个答案:

答案 0 :(得分:0)

试试这个

 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");

或者你可以试试这个整合youtube api 是!查看Android Youtube播放器API和YouTube API。

Android Player:

YouTube API:

YouTube Player API Sample

and follow this link to get video from channel

答案 1 :(得分:0)

根据您的问题,您尚未添加YouTube视频链接。您正在使用youtube频道网址。无论如何试试这个。效果很好。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String frameVideo = "<html><body><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/PMBcMBhsm80\" 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");
}

答案 2 :(得分:0)

首先启用

webview.getSettings().setJavaScriptEnabled(true);

然后

如果单击WebView网页内的任何链接,该页面将不会加载到WebView中。为此,您需要从WebViewClient扩展您的类并覆盖其方法。它的语法是:

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

答案 3 :(得分:0)

是的,我找到了这个问题的解决方案, 在这里解决方案,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_bottom7);
    webView = (WebView)findViewById(R.id.videoView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl("https://www.youtube.com/channel/UCLmI8_hVUqA251PzK7OSvQw");

    webView.setWebViewClient(new loadWebView());
}
private class loadWebView extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

需要添加

webSetting.setDomStorageEnable(true);

对我来说工作正常!!!!!