Launch the media player using the link, click in WebView

I created an application that loads a website using WebView. Everything works fine, except that I cannot play video files in the application. So, what I'm looking for helps get my application to launch the media player when I click on the link ending in .mp4. Any help and advice would be greatly appreciated!

+5
source share
2 answers

You need to override the method of shouldOverrideUrlLoadingyour webViewCient ...

final class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp4") {
            Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(url));
            view.getContext().startActivity(intent);
            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
}

... which you assign to your webview:

WebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(webViewClient);

Edit: It is also important to add:

webView.getSettings().setAllowFileAccess(true);

to allow downloading files / streams such as mp4 files.

+5
source

. , . , , Android 4.0 (Ice Cream Sandwich). :

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/*");
startActivity(intent);`
+2

All Articles