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!
+1
A:
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 Lin
2010-08-01 05:08:23
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
2010-08-01 05:55:06
Add webView.getSettings().setAllowFileAccess(true); That will help.I just tried it in my code and it works.
Mathias Lin
2010-08-01 07:54:51
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
2010-08-02 01:52:26
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 Lin
2010-08-02 01:58:31
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
2010-08-02 02:08:03
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 Lin
2010-08-02 02:17:46
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
2010-08-02 02:21:56
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
2010-08-02 02:27:51
hm not much to see from the log line (error or warning). please try the things in my last comment.
Mathias Lin
2010-08-02 02:34:57
I will give it a try and research some more. If i get a solution, I'll let you know. Thanks again!
Kyle
2010-08-02 02:36:39
Mathias,I've come to the conclusion its a problem with .mp4 files. I can play .3gp files without any problems!
Kyle
2010-08-02 06:04:11
Would it be possible to if (url.endsWith(".mp4") allow the url to be loaded from the web browser and not the application?
Kyle
2010-08-02 17:20:09
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 Lin
2010-08-02 17:48:26
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
2010-08-02 17:59:20
Mathias, Thanks for all your help and guidance. I've been able to get it to work the way it should. Thanks again!
Kyle
2010-08-02 21:42:27
Cool. how did you finally get it to run?
Mathias Lin
2010-08-03 11:21:15
Honestly 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
2010-08-03 18:32:51