5

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!

Genadinik
  • 18,153
  • 63
  • 185
  • 284

3 Answers3

11

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.

Kai
  • 15,284
  • 6
  • 51
  • 82
  • 1
    But the problem using Youtuebe Android player is that u need to know the video id and youtube app must be installed in your device. what about my problem ? http://stackoverflow.com/questions/18533678/playing-youtube-videos-smoothly-in-web-view – anshul Sep 03 '13 at 12:49
  • From Android 5, and with the WebChromeClient, everything works fine. But in Android 4 versions, the error can be different. Here i tried to catalog all: http://stackoverflow.com/questions/31591010/youtube-iframe-player-wont-show-videos-in-android-4-x-webview/38410424#38410424 – jos Jul 16 '16 at 11:11
1

Have you tried using your own WebChromeClient? This question seems relevant. WebView and HTML5 <video>

You'll need to create one of these and implement it yourself, similar to what you're doing for the WebViewClient.

webview.setWebChromeClient(new WebChromeClient() {
    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        super.onShowCustomView(view, callback);
        if (view instanceof FrameLayout) {
            FrameLayout frame = (FrameLayout) view;
            if (frame.getFocusedChild() instanceof VideoView) {
                VideoView video = (VideoView) frame.getFocusedChild();
                frame.removeView(video);
                a.setContentView(video);
                video.setOnCompletionListener(this);
                video.setOnErrorListener(this);
                video.start();
            }
        }
    }
});

Make sure you add webview.getSettings().setPluginsEnabled(true); to your webview settings.

And most importantly, for any webpage to load in a WebView, be sure to have the INTERNET permission in your Manifest:

<uses-permission android:name="android.permission.INTERNET"/>
Community
  • 1
  • 1
ggmathur
  • 833
  • 5
  • 12
0

Use

<application
        android:hardwareAccelerated="true"
        android:allowBackup="true">
</application>

Perfectly working.