Reputation: 45
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 ?
Upvotes: 4
Views: 4937
Reputation: 6342
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
Upvotes: 2