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!
Asked
Active
Viewed 7,632 times
2 Answers
5
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.

Mathias Conradt
- 28,420
- 21
- 138
- 192
-
Thanks for the quick response! I have added the code to my application and it compiles without error..But it still doesn't load the mediaplayer when the link is clicked. – Kyle Aug 01 '10 at 05:55
-
Add webView.getSettings().setAllowFileAccess(true); That will help. I just tried it in my code and it works. – Mathias Conradt Aug 01 '10 at 07:54
-
Hey Mathias, Thank you very much for all your help! I've been able to get it working on the Emulator. I tried to load the app on my phone running 2.2 and it launches the web browser and then closes, putting me right back to my app. No video at all. I'm not really sure what could be wrong, other than I'm running the Emulator on 1.5. What do you think? – Kyle Aug 02 '10 at 01:52
-
Are you sure the mp4 is Android compatible (since there might be some differences between mp4 encoding). Any error in the logfile (adb shell logcat). Please try to open this mp4 for a test from within your webview: http://www.law.duke.edu/cspd/contest/finalists/entries/documentariesandyou.mp4 It's used in many samples and for sure works in Android. (btw: I also tested on Nexus One 2.2 without a problem.) – Mathias Conradt Aug 02 '10 at 01:58
-
Ok, I take it back..I'm able to get the Media Player to work on the emulator but all I get is sound, no video. When I try to run the app on my phone all i get it the browser opening and closing, no sound or video. I tried to play that mp4 you linked and i get sound no video. hmmm – Kyle Aug 02 '10 at 02:08
-
Ok, it sounds like a common problem, but I'm not sure about the resolution in this case. So what happens if you open the mentioned mp4 right in the android mediaplayer instead of launching it via your app. Does that play the video? It's a common issue that you have, I saw that often in the Android dev group, also here on stackoverflow, i.e. http://stackoverflow.com/questions/2184364/android-video-hear-sound-but-no-video, but in you case it's different cause you have the issue both on device and emulator. Please try to open the mp4 in the mediaplayer directly and also in the default browser. – Mathias Conradt Aug 02 '10 at 02:17
-
Here is the log of when the phone views the mp4 VIEW dat=http://***.com/security/*******-480x272_H264.mp4 typ=video/mp4 cmp=com.cooliris.media/.MovieView }D/webviewglue( 8409): nativeDestroy view: 0x5040c8I/ActivityManager( 1317): moveTaskToBack: 37 W/InputManagerService( 1317): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45524198 – Kyle Aug 02 '10 at 02:21
-
Hmm im not really sure either...I am able to play the mp4 file you posted a couple posts up with no problem, if I load it directly. – Kyle Aug 02 '10 at 02:27
-
hm not much to see from the log line (error or warning). please try the things in my last comment. – Mathias Conradt Aug 02 '10 at 02:34
-
I will give it a try and research some more. If i get a solution, I'll let you know. Thanks again! – Kyle Aug 02 '10 at 02:36
-
Mathias, I've come to the conclusion its a problem with .mp4 files. I can play .3gp files without any problems! – Kyle Aug 02 '10 at 06:04
-
Would it be possible to if (url.endsWith(".mp4") allow the url to be loaded from the web browser and not the application? – Kyle Aug 02 '10 at 17:20
-
On my nexus one, when i open the mp4 from my app webview, the default intent for the mp4 http url will be started, which seems to be the default web browser, which I see for a second or so, which then redirects further to the system default media player. That's how it is on my Nexus One 2.2 FRF91 (final stable Froyo release). I can see the video image and hear the sound. – Mathias Conradt Aug 02 '10 at 17:48
-
I wonder why when i try to play a mp4 file from my webview, it opens the browser then immediately exits and doesn't play anything. But if I go to the same site and click the same link directly from the browser, it opens the media player and plays everything perfect. – Kyle Aug 02 '10 at 17:59
-
Mathias, Thanks for all your help and guidance. I've been able to get it to work the way it should. Thanks again! – Kyle Aug 02 '10 at 21:42
-
-
1Honestly all i did was reboot the phone a few times..And now its working perfect each time i try it. i have no clue. – Kyle Aug 03 '10 at 18:32
2
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);`

Chris
- 21
- 1