In android playing videos from web-view is such a mess but now just using a small trick you can play all web videos of the YouTube, Daily-motions, etc.
You just have to add this method in your web-view Activity.
Here is the trick-:
mWebView.setWebChromeClient(new WebChromeClient() {
public CustomViewCallback mCustomViewCallback;
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
FrameLayout customViewContainer = (FrameLayout) view;
mCustomViewCallback = callback;
if (customViewContainer.getFocusedChild() instanceof VideoView) {
VideoView customVideoView = (VideoView) customViewContainer.getFocusedChild();
try {
Field mUriField = VideoView.class.getDeclaredField("mUri");
mUriField.setAccessible(true);
Uri uri = (Uri) mUriField.get(customVideoView);
Log.w("uri", ""+uri);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
startActivity(intent);
new Handler().post(new Runnable() {
@Override
public void run() {
mCustomViewCallback.onCustomViewHidden();
}
});
} catch (Exception e) {
}
}
}
}
});
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"/>**
I hope this will help CHEERS
0 Comment(s)