如何在Android webview中播放全屏YouTube视频?

时间:2011-11-22 11:05:26

标签: android youtube android-webview

我是android开发人员的新手。我有两个问题:

  1. 点击播放标志后如何立即制作全屏视频?
  2. 当正常大小的视频和用户想要滚动具有标题和静态模式的页面时,视频将覆盖标题。当用户将其滚动到标题时,它应该在标题下方。当用户滚动页面直到标题位置时,如何在标题下面制作视频?
  3. 这是我的代码:

            String widthAndHeight = "width='220' height='200'";
            String videoURL = "http://www.youtube.com/v/AyeJyctGhSc&feature=youtube_gdata";
    
            String temp = "<object "+widthAndHeight+">" +
            "<param name='allowFullScreen' value='false'>" +
            "</param><param name='allowscriptaccess' value='always'>" +
            "</param><embed src='"+ videoURL +"'" +
            " type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true'" + widthAndHeight +
            "></embed></object>";
    
            video.getSettings().setPluginState(PluginState.ON);
            video.getSettings().setJavaScriptEnabled(true);
            video.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
            video.getSettings().setPluginsEnabled(true);
            video.getSettings().setSupportMultipleWindows(false);
            video.getSettings().setSupportZoom(false);
            video.setVerticalScrollBarEnabled(false);
            video.setHorizontalScrollBarEnabled(false);
            video.loadData(temp,"text/html", "utf-8");
    

3 个答案:

答案 0 :(得分:2)

使用此来源播放Youtube视频

        String video = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 100%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
                + youtubeId +
                "?autoplay=1"
                + "&fs=0\" frameborder=\"0\">\n"
                + "</iframe>\n";
        mWebview.getSettings().setPluginState(PluginState.ON);
        mWebview.setWebChromeClient(new WebChromeClient());
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.setHorizontalScrollBarEnabled(false);
        mWebview.setVerticalScrollBarEnabled(false);
        mWebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mWebview.getSettings().setBuiltInZoomControls(false);
        mWebview.getSettings().setAppCacheEnabled(true);
        mWebview.setInitialScale(0);
        mWebview.getSettings().setLoadWithOverviewMode(true);
        mWebview.getSettings().setUseWideViewPort(true);
        mWebview.loadData(video,"text/html","UTF-8");

答案 1 :(得分:0)

播放YouTube视频

你必须解析网址并在videoview中播放视频

答案 2 :(得分:0)

在您的活动中添加以下代码:

WebView.setWebChromeClient(new WebChromeClient()

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            customComponenet.addView(view);
            mWebView.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onHideCustomView() {
            if (customComponenet == null)
                return;
            // Hide the custom view.
            customComponenet.setVisibility(View.GONE);

            mWebView.setVisibility(View.VISIBLE);
        }
    });

其中customComponent是你的FrameLayout。