I have some WebView code with which I am trying to play YouTube videos on a YouTube channel. But all it is doing is showing the spinner icon on a video and never actually starting the video. Would anyone know how to fix that?
public class YoutubeActivity extends Activity
{
WebView webview = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("http://www.youtube.com/g33ktalktv");
}
public void onBackPressed ( )
{
//Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
//invoke(html5WebView, (Object[]) null);
webview.clearView();
}
@Override
public void onStop()
{
super.onStop();
// your code
webview.clearView();
}
}
And this is the manifest setting:
<activity
android:name="YoutubeActivity"
android:label="Some string"
android:hardwareAccelerated="true"/>
Thannk in advance!
This interface was deprecated in API level 12. This interface is now obsolete.
getSettings(). setBuiltInZoomControls(false); webView. getSettings(). setSupportZoom(false); webView.
You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.
Using WebView to play YouTube videos would require extensive testing and debugging on different Android OS versions due to the difference in functionality and bugs between Android 2.x and 4.x.
A less bug-prone approach that gives you more control is to use YouTube Android Player API to embed a YouTube video into your own app, they have sample app so it shouldn't be too difficult if you follow their steps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With