4

Is it possible to play a youtube video from a webview, same like when I access the youtube website from a browser and the video starts playing it plays the video in a full screen.

Here is my code so far :

webView = (WebView) v.findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient()); 
webView.setWebViewClient(new WebViewClient()); 
webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.setWebChromeClient(new WebChromeClient() {
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                final FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    // get video view

                    VideoView video = (VideoView) frame.getFocusedChild();
                    Uri mUri = null;
                    try {
                        Field mUriField = VideoView.class.getDeclaredField("mUri");
                        mUriField.setAccessible(true);
                        mUri = (Uri) mUriField.get(video);
                        Log.d(TAG, "the uri is "+mUri.toString());
                        Intent i = new Intent();
                        i.setData(mUri);
                        i.setAction(Intent.ACTION_VIEW);
                        startActivity(i);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    });

webView.loadUrl("www.youtube.com");

I want to start the youtube app only when the user clicks on 'play' on a youtube video inside the webview, not before.

The whole channel needs to be displayed, so the user can choose which video he wants to play, same behavior as accessing the youtube website from the browser applcation.

Edit: as suggested I have added onShowCustomView() method to handle the play click, now I get some uri, how can I play the video in a full screen videoview ?

user22098
  • 45
  • 1
  • 5
  • It means that you do not want to play the video in `webView` ? if yes then you can use a `overlay` view and set a `onClick` listener on that view to open the `youtube` app. – Ali Imran Dec 30 '12 at 07:54
  • I want to know which video was clicked, so I can start playing the youtube video with an intent. – user22098 Dec 30 '12 at 08:14

1 Answers1

2
webView = (WebView) findViewById( R.id.webview_1 );

String play= "<html><body>Youtube <br> <iframe class=\"youtube-player\" type=\"text/html\" width=\"600\" height=\"300\" src=\"http://www.youtube.com/embed/bIPcobKMB94\" frameborder=\"0\"></body></html>"

webView.loadData(play, "text/html", "utf-8");

Also can see..
YouTube Video not playing in WebView - Android

Community
  • 1
  • 1
ridoy
  • 6,274
  • 2
  • 29
  • 60
  • I want to display a whole channel, and I still want the user to be able to navigate in the youtube site. – user22098 Dec 30 '12 at 08:10
  • I'm not sure whether you can nevigate inside a webview.see it for some information..http://stackoverflow.com/questions/12572549/how-do-i-display-youtube-site-video-within-webview – ridoy Dec 30 '12 at 08:16
  • overriding onShowCustomView helped. – user22098 Dec 30 '12 at 08:54