在 Web 视图中播放应用程序本地视频 (.mp4)

发布于 2024-12-09 04:42:49 字数 7839 浏览 0 评论 0 原文

我正在为 iOS 和 Android 编写一个应用程序。我有一个内容页面,需要在 UIWebView/WebView 中链接到/播放 .mp4 视频。该视频将包含在应用程序中,以便它可以离线工作。

在 iOS 中,我只使用了一个视频标签(带有项目文件的源代码),然后使用 NSNotifications 来全屏和横向播放视频(应用程序通常以纵向模式运行)。工作完成了。

在 Android 上我遇到了一些问题。

首先,当您使用本地图像作为视频海报时,它会使应用程序崩溃(显然是一个已知的错误 - 使用远程图像解决了问题 - 尽管对离线模式没有太大帮助)。

其次就是不会玩。我尝试过远程文件和本地文件(引用为 file:///android_asset/html/video.mp4)。

我已经从 Android 浏览器测试了 .mp4,它播放正常,所以我知道视频没问题。

我尝试了另一种方法,使用视频链接。我在以下链接找到了一个示例(必须使用谷歌缓存才能查看):http://webcache.googleusercontent.com/search?q=cache:EVz7nwc8718J:www.codelark.com/2010/05/12/android-viewing-video-from-em bedded-webview/+http://www.codelark.com/2010/05/12/android-viewing-video-from-embedded-webview/&cd=1&hl=en&ct=clnk&gl=uk

我的代码如下:

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"); 

使用远程视频链接,它尝试打开 Android 浏览器,然后返回到应用程序。使用本地视频链接,它似乎会加载播放器,但我得到“无法播放视频 - 抱歉,该视频无法播放”

这是我的 logcat 日志:

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

我还在 shouldOverrideUrlLoading 中尝试了上述方法,它具有相同的结果。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌无敌 2024-12-16 04:42:49

我陷入了类似的问题。 Finalyl 我通过使用“视频”HTML5 标签让事情正常工作

  • Android 操作系统中有一些东西阻止从以下位置打开视频
    资产。我必须将 html 和视频复制到 SDCard。然后,您可以使用 sdcard 路径而不是资产路径。
  • 不要忘记为您的 webview 活动使用 android:hardwareAcceleterated = "true"

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文