如何在WebView Android上自动播放iframe YouTube [英] How to auto play iframe youtube on webview android

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

问题描述

我在我的应用中使用android webview来显示来自youtube的视频,效果很好.

I use android webview in my app for display video from youtube it's work well.

但是我希望它是自动播放的.

But i want it's auto play.

这是我的活动,我将?autoplay = 1" 附加到videoUrl,但是没有任何变化,它仍然不是自动播放.

This is my activity i append "?autoplay=1" to videoUrl but nothing change it's still not auto play.

public class LandVideoAct extends Activity {
    WebView mWebView;
    String videoURL = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.landfull);
        videoURL = "http://www.youtube.com/embed/VYIi49czywo?autoplay=1";
        mWebView = (WebView) findViewById(R.id.fullwebview);

        String vid = "<html><body style=\"margin: 0; padding: 0\"><iframe  width=\"100%\" height=\"100%\" src=\""+videoURL+"\" type=\"text/html\" frameborder=\"0\"></iframe><body><html>";

        WebChromeClient mWebChromeClient = new WebChromeClient(){
            public void onProgressChanged(WebView view, int newProgress) {
            }
        };

        mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
        mWebView.setWebChromeClient(mWebChromeClient);
        mWebView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                mWebView.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); 
            }
        });
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.setInitialScale(1);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        if (Build.VERSION.SDK_INT < 17) {
            Log.i("GPSNETWORK", "<17");
        } else {
            Log.i("GPSNETWORK", Build.VERSION.SDK_INT+">=17");
            mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
        }
        mWebView.loadData(vid, "text/html", "UTF-8");
    }
}

推荐答案

我认为在webview或android浏览器中是不可能的.要实现自动播放,我认为您需要"YOUTUBE API".

I think its not possible in webview or android browsers. To achieve auto playing, I think you need "YOUTUBE API".

检查以下链接:

1] 没有办法在Android平台上自动播放视频?

2] Youtube Api android自动启动

以上这些链接将使您对自动播放以及youtube api有所了解.

These above links will give you idea about auto playing as well as youtube api.

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

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