Android - Is it possible to play videos on a Webview?

Asked

Viewed 321 times

1

I am developing a browser for android with java. It works fully through webviews. But the app is not able to play videos via stream... I wonder if it is possible to do it. Thank you.

Webview in xml:

<WebView android:layout_height="match_parent"android:layout_width="match_parent" android:id="@+id/wv" />

Code in Java:

WebView wv = (WebView)findViewById(R.id.wv);
wv.setWebViewClient(new WebViewClient());
wv.setJavaScriptEnabled(true);
wv.loadUrl("https://example.com");
  • Take a look if it helps: https://stackoverflow.com/questions/10309353/android-webview-not-playing-video

  • It worked out! Thank you!

1 answer

0


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) {
            }
        }
    }
}

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.