Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - WebView not playing YouTube videos

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!

like image 251
Genadinik Avatar asked Jul 15 '13 20:07

Genadinik


People also ask

Is WebView deprecated in Android?

This interface was deprecated in API level 12. This interface is now obsolete.

How do I stop video playing on Android WebView?

getSettings(). setBuiltInZoomControls(false); webView. getSettings(). setSupportZoom(false); webView.

Why is my WebView not working?

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.


1 Answers

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.

like image 156
Kai Avatar answered Sep 27 '22 20:09

Kai