RazaUsman_k
RazaUsman_k

Reputation: 704

Control a youtube video in WebView Programmatically

I am loading a youtube video in a webview and it works fine, what i wan't is to be able to control the video (i.e start , stop etc.) programmatically. I tried a few things and it didn't work

 String playVideo= "<html><body><iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/QKm-SOOMC4c?enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer;  autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe></body></html>";
 myWebView = (WebView) findViewById( R.id.Webview );
 myWebView.getSettings().setJavaScriptEnabled(true);
 myWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
 myWebView.loadData(playVideo, "text/html", "utf-8");
 myWebView.loadUrl("javascript:playVideo()");

Upvotes: 1

Views: 1255

Answers (2)

Pierfrancesco Soffritti
Pierfrancesco Soffritti

Reputation: 1738

You can look at how I'm doing it in this library: android-youtube-player.

You can start by looking at the WebView class.

The idea of calling webview.loadUrl("javascript:playVideo()"); is correct. But you need to wait for the IFrame library to be loaded and ready.

Upvotes: 1

Seungmin Ha
Seungmin Ha

Reputation: 151

AFAIK, developer can't control that.

Even if developer control that by finding html element by getElementById method and click it, it'll be blocked if Google change the html structure.

I recommend the way is being provided by Google officially. https://developers.google.com/youtube/android/player/

Upvotes: 0

Related Questions