Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Play YouTube videos and other Webview videos in Android.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.55k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: