Karan_Rana
Karan_Rana

Reputation: 2823

Android webview not playing video

In my application there are webviews and videos within webviews.But video is not playing .. Below is snippet of webview code..

Web.setWebViewClient(new myWebClient()); 
Web.getSettings().setJavaScriptEnabled(true);
Web.getSettings().setPluginsEnabled(true);
Web.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");

Upvotes: 0

Views: 6782

Answers (1)

Thomas G.
Thomas G.

Reputation: 882

After 2 days, looking for a lite way to do it, here is, for me, the best and simpliest solution :

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);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, "video/*");
                mActivity.startActivity(intent);
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        mCustomViewCallback.onCustomViewHidden();
                    }
                });
            } catch (Exception e) {
            }
        }
    }
}

Hope this help...

PS : Works with YouTube

Upvotes: 2

Related Questions