1

我正在 webview 中加载 youtube 视频,但问题是视频没有像我们访问实际的 youtube 网站时那样自动播放。在 webview 中,视频确实显示并播放,但只有在我按下播放按钮之后出现在视频的顶部。所以我想知道是否有任何方法可以在加载 URL 后在 webview 中播放视频。以下是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_view);
    WebView wv = (WebView) findViewById(R.id.webView);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.getSettings().setPluginState(PluginState.ON);
    final String mimeType = "text/html";
    final String encoding = "UTF-8";
    String html = getHTML();
    wv.setWebChromeClient(new WebChromeClient() {
    });
    wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}

public String getHTML() {
     String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
                + "J2fB5XWj6IE"
                + "?fs=0\" frameborder=\"0\">\n"
                + "</iframe>\n";
        return html;
}

谢谢你!!!

4

3 回答 3

4

将“autoplay=1”添加到您的网址

String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
            + "J2fB5XWj6IE?autoplay=1"
            + "&fs=0\" frameborder=\"0\">\n"
            + "</iframe>\n";
于 2012-11-26T19:25:27.013 回答
0

Doing Youtube is kind of tricky. First you will need try to access the mobile site. Second you need to play it in a popup window. The playback needs to be done in the WebChromClient#onShowCustomView method. I kind of forgot the details but that is the general idea. The "view" parameter of the onShowCustomView method, if I remember correctly is a layout with a video view in it, you can start it to play.

于 2012-11-26T19:29:54.543 回答
0

Follow this code, your video will run well

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setPluginState(PluginState.ON);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setAllowFileAccess(true);
    //webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

    webView.setBackgroundColor(Color.BLACK);

    webView.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36");
    //webView.addJavascriptInterface(this, "nativeInterface");

    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }
    webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


    webView.setWebViewClient(new WebViewClient());

    webView.setWebChromeClient(new WebChromeClient());

    String youtubeID = ""; //get Youtube ID and input here

    String url = "http://www.youtube.com/embed/" +
            youtubeID +
            "?autoplay=1";

    webView.loadUrl(url);
于 2014-02-28T03:17:52.297 回答