Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing an app local video (.mp4) in a webview

I have an app that I'm writing for both iOS and Android. I have a content page with which I need to link to/play a .mp4 video in a UIWebView/WebView. The video is going to be included in the app so that it can work offline.

With iOS I just used a video tag (with source to a project file) and then used NSNotifications to fullscreen and landscape the video (app normally runs in portrait mode). Job done.

On Android I'm having some issues.

Firstly, when you use a local image as the video poster it crashed the app (apparently a known bug - using a remote image solves the problem - although not much help for an offline mode).

Secondly it doesn't play. I've tried both a remote and local file (referenced as file:///android_asset/html/video.mp4).

I've tested the .mp4 from the android browser and it plays okay so I know that the video is fine.

I've tried a different tack using a link to a video instead. I found an example at the following link (have to use google cache to view): http://webcache.googleusercontent.com/search?q=cache:EVz7nwc8718J:www.codelark.com/2010/05/12/android-viewing-video-from-embedded-webview/+http://www.codelark.com/2010/05/12/android-viewing-video-from-embedded-webview/&cd=1&hl=en&ct=clnk&gl=uk

My code is as follows:

webengine = (WebView) findViewById(R.id.webview);
webengine.getSettings().setJavaScriptEnabled(true);
webengine.getSettings().setPluginsEnabled(true); 
webengine.getSettings().setAllowFileAccess(true);
webengine.setDownloadListener(new DownloadListener()
{
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType, long size)
    {
        Intent viewIntent = new Intent(Intent.ACTION_VIEW);
        viewIntent.setDataAndType(Uri.parse(url), mimeType);     
    try
    {
         startActivity(viewIntent);
    }
    catch (ActivityNotFoundException ex)
    {
         Log.w("sta", "Couldn't find activity to view mimetype: " + mimeType);
    }
}

});
webengine.loadUrl("file:///android_asset/html/index.html"); 

With a remote video link it tries to open the android browser and then bails back to the app. With a local video link it appears to load the player but I get "Cannot play video - Sorry, this video cannot be played"

This is my logcat log:

I/ActivityManager(  168): Starting: Intent { act=android.intent.action.VIEW dat=file:///android_asset/html/standrewsanimation.mp4 typ=video/mp4 cmp=com.cooliris.media/.MovieView } from pid 4299
W/ActivityManager(  168): Trying to launch com.cooliris.media/.MovieView
I/WindowManager(  168): Setting rotation to 1, animFlags=1
I/ActivityManager(  168): Config changed: { scale=1.0 imsi=234/20 loc=en_GB touch=3 keys=1/1/2 nav=1/1 orien=2 layout=18 uiMode=17 seq=40}
D/PhoneApp(  301): updateProximitySensorMode: lock already released.
I/ApplicationPackageManager( 3982): cscCountry is not German : H3G
D/MovieView( 3982): onCreate
I/MovieView( 3982): registerBroadcastRecievers
D/MovieViewControl( 3982): getFilePathByUri URI : file:///android_asset/html/standrewsanimation.mp4
D/MovieViewControl( 3982): getFilePathByUri path from DB - Path : null
D/MovieViewControl( 3982): getFilePathByUri path from uri - Path : /android_asset/html/standrewsanimation.mp4
D/MovieViewControl( 3982): checkDRMContent() path : /android_asset/html/standrewsanimation.mp4 URI : file:///android_asset/html/standrewsanimation.mp4
W/System.err( 3982): java.io.FileNotFoundException: File is not present
W/System.err( 3982):    at android.drm.mobile2.OMADRMManager.IsDrmFileByExt(OMADRMManager.java:1032)
W/System.err( 3982):    at com.cooliris.media.VideoDRMUtil.checkIsOMADrm(VideoDRMUtil.java:169)
W/System.err( 3982):    at com.cooliris.media.MovieViewControl.checkDRMContent(MovieViewControl.java:236)
W/System.err( 3982):    at com.cooliris.media.MovieViewControl.<init>(MovieViewControl.java:168)
W/System.err( 3982):    at com.cooliris.media.MovieView$1.<init>(MovieView.java:68)
W/System.err( 3982):    at com.cooliris.media.MovieView.onCreate(MovieView.java:68)
W/System.err( 3982):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 3982):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
W/System.err( 3982):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
W/System.err( 3982):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
W/System.err( 3982):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
W/System.err( 3982):    at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 3982):    at android.os.Looper.loop(Looper.java:123)
W/System.err( 3982):    at android.app.ActivityThread.main(ActivityThread.java:3687)
W/System.err( 3982):    at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 3982):    at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 3982):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
W/System.err( 3982):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
W/System.err( 3982):    at dalvik.system.NativeStart.main(Native Method)
I/VideoView( 3982): start()
D/MovieView( 3982): onStart
D/MovieView( 3982): onResume
D/MovieViewControl( 3982): onResume()
D/VideoView( 3982): onMeasure()
I/VideoView( 3982):     Setting size: 480x320
D/VideoView( 3982): onMeasure()
I/VideoView( 3982):     Setting size: 480x320
V/AudioPolicyManager(   95): stopOutput() output 1, stream 1, session 301
V/AudioPolicyManager(   95): getNewDevice() selected device 0
V/AudioPolicyManager(   95): getNewDevice() selected device 0
V/AudioPolicyManager(   95): setOutputDevice() output 1 device 0 delayMs 150
V/AudioPolicyManager(   95): setOutputDevice() setting same device 0 or null device for output 1
I/ActivityManager(  168): Displayed com.cooliris.media/.MovieView: +287ms
V/PVPlayer(   95): PVPlayer constructor
V/PVPlayer(   95): construct PlayerDriver
V/PlayerDriver(   95): constructor
V/PlayerDriver(   95): OpenCore hardware module loaded
V/PlayerDriver(   95): start player thread
V/PlayerDriver(   95): startPlayerThread
V/PlayerDriver(   95): InitializeForThread
V/PlayerDriver(   95): OMX_MasterInit
V/PlayerDriver(   95): OsclScheduler::Init
V/PlayerDriver(   95): CreatePlayer
V/PlayerDriver(   95): AddToScheduler
V/PlayerDriver(   95): PendForExec
V/PlayerDriver(   95): OsclActiveScheduler::Current
V/PlayerDriver(   95): StartScheduler
V/PVPlayer(   95): send PLAYER_SETUP
V/PlayerDriver(   95): Send player code: 2
V/PlayerDriver(   95): CommandCompleted
V/PlayerDriver(   95): Completed command PLAYER_SETUP status=PVMFSuccess
V/PVPlayer(   95): setDataSource(/android_asset/html/standrewsanimation.mp4)
V/PVPlayer(   95): setVideoSurface(0x5dbb8)
V/PVPlayer(   95): setVideoSurface(0x5f1d0)
V/PVPlayer(   95): prepareAsync
V/PVPlayer(   95):   data source = /android_asset/html/standrewsanimation.mp4
V/PlayerDriver(   95): Send player code: 3
V/PlayerDriver(   95): handleSetDataSource
V/PlayerDriver(   95): handleSetDataSource- scanning for extension
V/PlayerDriver(   95): HandleInformationalEvent: PVMFInfoErrorHandlingStart
V/PlayerDriver(   95): HandleInformationalEvent: type=26 UNHANDLED
W/MediaPlayer( 3982): info/warning (1, 26)
V/PlayerDriver(   95): CommandCompleted
V/PlayerDriver(   95): Completed command PLAYER_SET_DATA_SOURCE status=PVMFErrNotSupported
E/PlayerDriver(   95): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported
E/MediaPlayer( 3982): error (1, -4)
V/PVPlayer(   95): run_init s=-2147483648, cancelled=0
V/PlayerDriver(   95): HandleInformationalEvent: PVMFInfoErrorHandlingComplete
W/PlayerDriver(   95): PVMFInfoErrorHandlingComplete
I/PowerManagerService(  168): Ulight 3->3|12
I/MediaPlayer( 3982): Info (1,26)
E/MediaPlayer( 3982): Error (1,-4)
D/VideoView( 3982): Error: 1,-4
D/VideoView( 3982): onMeasure()
I/VideoView( 3982):     Setting size: 480x320
D/VideoView( 3982): onMeasure()
I/VideoView( 3982):     Setting size: 480x320

I've also tried the above approach in shouldOverrideUrlLoading which has the same result.

Thanks

like image 391
Brad Avatar asked Oct 12 '11 11:10

Brad


1 Answers

I was stuck in a similar problem. Finalyl I got the thing working by using "video" HTML5 tag

  • There is something in android OS that prevent opening the video from assets. I had to copy the html and video to SDCard. You can then use the sdcard path instead of assets path.
  • Dont forget to use android:hardwareAcceletrated = "true" for your webview activity
like image 186
Saurabh Wadhwa Avatar answered Oct 04 '22 22:10

Saurabh Wadhwa