Android:在 Webview 上播放 YouTube 视频 [英] Android: playing youtube video on Webview

查看:87
本文介绍了Android:在 Webview 上播放 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WebView 上播放 YouTube 视频时遇到问题.我整天都在阅读有关如何执行此操作的问题和答案,但它不起作用.我已经设置了清单,这是我正在使用的代码

i've a problem to playing youtube video on a WebView. It's all day i reading question and answer on how to do this, but it doesn't work. I've already set the Manifest and this is the code i'm using

mWebview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });


    mWebview.getSettings().setPluginState(WebSettings.PluginState.ON);
    mWebview.setWebChromeClient(new WebChromeClient());
    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.getSettings().setAppCacheEnabled(true);
    mWebview.setInitialScale(1);
    mWebview.getSettings().setLoadWithOverviewMode(true);
    mWebview.getSettings().setUseWideViewPort(true);

    //webSettings.setLoadsImagesAutomatically(true);
    //webSettings.setSupportZoom(false);

    mWebview.loadUrl("https://www.youtube.com/embed/MYVIDEOID");

发生的事情是这个我可以点击播放按钮计时器开始,但我什么也看不见.

and what's happen is this i can click the play button the timer start but i can see nothing.

问题似乎出现在模拟器上而不是真实设备上,请查看评论以获取答案.

The problem seems on the emulator and not on real device, check the comment for the answer.

推荐答案

试试这个

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:hardwareAccelerated="true"

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

</application>

这篇关于Android:在 Webview 上播放 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆