Play local video application (.mp4) in web view

I have an application that I am writing for iOS and Android. I have a content page that I need to link / play .mp4 video to UIWebView / WebView. The video will be included in the application so that it can work offline.

In iOS, I just used the video tag (with the source for the project file), and then used NSNotifications for full-screen viewing and landscape of the video (the application usually works in portrait mode). Completion of the assignment.

I have problems on Android.

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

Secondly, he does not play. I tried both the remote and the local file (link to file: ///android_asset/html/video.mp4).

I tested .mp4 from an Android browser and it works well, so I know the video is fine.

I tried a different approach using the video link. I found an example at the following link (you need to use google cache to view it): 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"); 

Using remote video calling, she tries to open the Android browser and then returns the application. With local video, it seems to load the player, but I get "Can't play the 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 also tried the approach described above in shouldOverrideUrlLoading, which has the same result.

thanks

+6
source share
1 answer

I had a similar problem. Finalyl I got what works using the "video" HTML5 tag

  • There is something in the Android OS that prevents the opening of video from assets. I had to copy html and video to sdcard. Then you can use the sdcard path instead of the resource path.
  • Remember to use android: hardwareAcceletrated = "true" for your web browsing activity.
+1
source

Source: https://habr.com/ru/post/899126/


All Articles