Ytplayer没有在Android低版本上自动播放

时间:2015-04-09 08:13:40

标签: android youtube

我正在尝试使用ytplayer在webview中自动播放youtube视频(如下所示)。它在最新版本的棒棒糖中工作正常。但不是在棒棒糖下面的版本。我必须手动点击播放器才能播放视频。

HTML:

<!DOCTYPE html>
<html>
<head><style>body{margin:0px 0px 0px 0px;}</style></head>
<body>

    <div id="playertag"></div>

    <script type="text/javascript">
        var tag = document.createElement('script');
        tag.src = "http://www.youtube.com/player_api";
        var firstScriptTag = document.getElementsByTagName('script')[0];
        firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

        //    after the API code downloads.
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('playertag',
                                   {
                                   height: '%@',
                                   width: '%@',
                                   videoId: '%@', 
                                   allowfullscreen: 'true',
                                   playerVars: {
                                   "version":1,
                                    "autoplay": 1,
                                    "controls": 1,
                                     "showinfo": 0},    
                                   events:
                                   {
                                   'onReady': onPlayerReady,
                                   'onStateChange': onPlayerStateChange
                                   }});
        }

    // The API will call this function when the video player is ready.
    function onPlayerReady(event)
    {

        //event.target.playVideo();
        player.playVideo();

    }


    function onPlayerStateChange(event)
    {

            window.demo.sendValueFromHtml(event.data+'');

    }

      function getCurrentTime(){

        window.demo.getCurrentTime(player.getCurrentTime(),    player.getDuration());
    }

    function seekTo(progress){
        player.seekTo(progress, false);
    }

    </script>
</body>
</html>

Android代码:

        private void loadInWebview(Activity act){
        if(ytWebView==null)
            ytWebView = new WebView(act);
        ytWebView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        if(ytFrame.getChildCount()>0){
            ytFrame.removeAllViews();   
        }
        ytFrame.addView(ytWebView);
        try {

            InputStream is = act.getAssets().open("youtube.html");
            int size = is.available();

            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();

            String str = new String(buffer);
            str = str.replace("videoId: '%@'", "videoId: '"+videoYtId+"'");
            str = str.replace("height: '%@'", "height: '"+ytHeight+"'");
            str = str.replace("width: '%@'", "width: '"+ytWidth+"'");
            String mime = "text/html";
            String encoding = "utf-8";
            WebSettings mWebSettings = ytWebView.getSettings();  
            mWebSettings.setJavaScriptEnabled(true);  

            ytWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);

                    ytWebView.loadUrl("javascript:player.playVideo()"); 

                }
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    if(url.contains("v="+videoYtId))
                        return super.shouldOverrideUrlLoading(view, url);
                    else
                        return true;
                }
            });
            ytWebView.setWebChromeClient(new WebChromeClient() {

            });
            ytWebView.addJavascriptInterface(new MyJavaScriptInterface(), "demo");
            mWebSettings.setAppCacheEnabled(true);
            ytWebView.getSettings().setUserAgentString("Android");


            ytWebView.loadDataWithBaseURL("https://www.youtube.com", str, mime, encoding, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案