Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto play iframe youtube on webview android

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

But i want it's auto play.

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");
    }
}
like image 423
user2955394 Avatar asked Jul 25 '14 03:07

user2955394


People also ask

How do I get YouTube to play automatically in iframe?

To make an embedded video autoplay, add "&autoplay=1" to the video's embed code right after the video ID (the series of letters that follows "embed/"). Embedded videos that are autoplayed don't increment video views.

How do I Autoplay YouTube videos on Android?

Tap “Settings” to configure the in-app settings. Tap on the second option in the list, the one labelled “Autoplay”. Tap “Autoplay”, it will be the second item in the settings list. To toggle autoplay on or off, simply tap the slider so the setting is disabled or enabled as preferred.

How do I get YouTube app to play automatically?

Mobile app (within Settings) To do so, tap your profile picture > Settings > Autoplay. Within the Autoplay menu, you can tap the Autoplay next video slider to enable or disable the function.

How do I Autoplay YouTube videos on mobile?

Open the YouTube app on a mobile device or computer. Go to the screen for any video. At the top of the video player, tap on the Autoplay and toggle from off to on or vice-versa.


2 Answers

Check out this question. There are a couple of suggestions that may work for you, however the person who asked that question didn't say if it worked for them or not.

Most of what I'm reading is saying that most mobile platforms block autoplay to avoid poor user experience.

You can try this javascript code below:

var myvideo = document.getElementsByTagName('video')[0]; myvideo.play();

For Android 4.2.2+ try adding this in your native code:

WebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

Also check out this page. Which adds the code below to autoplay the video:

webview.setWebViewClient(new WebViewClient() {
        // autoplay when finished loading via javascript injection
        public void onPageFinished(WebView view, String url) { webview.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
    });
    webview.setWebChromeClient(new WebChromeClient());

    webview.loadUrl("http://html5demos.com/video");
}
like image 126
ShawnOrr Avatar answered Sep 29 '22 06:09

ShawnOrr


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

Check below link :

1] There's no way method to do autoplay video on android platform?

2] Youtube Api android autostart

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

like image 22
VVB Avatar answered Sep 29 '22 07:09

VVB