Search code examples
androidandroid-intentandroid-emulatorandroid-youtube-api

How to play youtube Embed videos on Android Device


I have a Youtube URL need to play on Android Device using WebView.

The URL is : http://www.youtube.com/embed/xSaFnED16is

I have used the below Code:

String video = extras.getString("video");
        Log.d("Video URL",video);
        String[] x = video.split("/");
        String host = x[2];
        Log.d("Host", host);
        String vid = x[3];
        watchMovieUrl = "http://www.youtube.com/embed/" + vid;
            // movie play by using webview
            myWebView = (WebView) findViewById(R.id.web);
            myWebView.getSettings().setPluginState(PluginState.ON);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);                
            myWebView.getSettings().setSupportMultipleWindows(false);
            myWebView.getSettings().setSupportZoom(false);
            myWebView.setVerticalScrollBarEnabled(false);
            myWebView.setHorizontalScrollBarEnabled(false);
            Log.d("MovieURL>>>>>",watchMovieUrl);
            myWebView.loadUrl(watchMovieUrl);

If i run my Code,I'm unable to play the video,WebView showing first look of video with play button, But after click on play button starting progress bar and it continues showing progress bar only? Could any one help?


Solution

  • Why do you use a WebView? It won't be needed if you use the YouTube API. More information in http://apiblog.youtube.com/2012/12/no-webview-required-with-native-youtube.html.

    Instead you can take a look at http://fancifulandroid.blogspot.com.es/2013/01/play-youtube-video-in-webview-without.html for information.