KKSINGLA
KKSINGLA

Reputation: 1344

Auto Play Youtube in Android Webview

Youtube autoplay tag is not working in Android Webview iframe. How to remove cc tag?

HTML need to load in webview:

<!DOCTYPE html>
<html>
    <style>
    .center{
        position: absolute;
        margin: auto auto;
        right: 0;
        left: 0;
        bottom: 0;
        top: 0;
    }

    </style>
    <body style="background:#000">
        <iframe id="videoContainer" class="center" width="100%" height="300"
                src="https://www.youtube.com/embed/Vt-Wf7d0CFo?rel=0&controls=1&showinfo=0&autoplay=1"
                frameborder="0" allow="autoplay; encrypted-media" allowfullscreen>
        </iframe>
    </body>
</html>

Same code working on the website.

Content variable holds the html mentioned above. Current Android Code:

 mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
       
        view.loadDataWithBaseURL(null, content, "text/html", "utf8", view.getUrl());

Upvotes: 1

Views: 1310

Answers (2)

Brent
Brent

Reputation: 778

Auto-play is disabled by default since Android SDK 17, you can set setMediaPlaybackRequiresUserGesture to false though to re-enable auto-play. Then you would also need to check the SDK version because this function isn't in earlier versions.

the check:

int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 16) {
    engine.getSettings().setMediaPlaybackRequiresUserGesture(false);
}

Upvotes: 3

Kshitiz shakya
Kshitiz shakya

Reputation: 25

seems you have wrong url format in src attribute

https://www.youtube.com/embed/5jnqWIlaLyA?rel=0&controls=1&showinfo=0&autoplay=1&cc_load_policy=3

Also check this url for the reference :

https://developers.google.com/youtube/player_parameters

Upvotes: 0

Related Questions