I have created an application that loads a website using WebView. Everything works fine except that I'm unable to play any video files within the application. So, what I'm looking for help on is getting my application to launch the mediaplayer when I click on a link ending with .mp4. Any help and tips would be much appreciated!
You need to override the shouldOverrideUrlLoading
method of your 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);
}
}
}
... that you assign to your webview:
WebViewClient webViewClient = new MyWebViewClient();
webView.setWebViewClient(webViewClient);
Edit: also important that you add:
webView.getSettings().setAllowFileAccess(true);
to allow file downloads/streams such as mp4 files.
Note: you may want to also set the data type. This appears to solve some issues in Android 4.0 (Ice Cream Sandwich). For example:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/*");
startActivity(intent);`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With