我们编写了一个嵌入Webview的大型Kotlin应用程序。就在今天,我们发现某些URL嵌入了一个<VIDEO>
播放列表URL的m3u8
标签。
页面加载后,就会出现很多错误,如果我们触摸播放图标,则什么也不会发生。有时甚至应用程序崩溃。
要找出问题,我们编写了一个仅带WebView的非常简单的应用程序,然后尝试加载Apple示例页面:
https://developer.apple.com/streaming/examples/basic-stream-osx-ios4-3.html
该应用程序已在多个物理设备上进行了测试。
这是相关的gradle部分
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 24
targetSdkVersion 28
我们在清单中添加了适当的权限
<uses-permission android:name="android.permission.INTERNET"/>
这是我们的MainActivity
package iakta.it.webviewtest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
testWebView.loadUrl("https://developer.apple.com/streaming/examples/basic-stream-osx-ios4-3.html")
}
}
Apple的页面加载正常(或者看起来,我们可以在屏幕上看到它),但是LogCat出现了很多错误,我们无法播放视频。
这些是日志中的一些错误。当然,我们的项目中没有res/raw/empty.wav
文件。它只是通过AndroidStudio向导从头开始创建的。
2019-04-17 18:34:45.168 9326-9326/iakta.it.webviewtest E/cr_crMediaCrashListener: Exception while creating the watchdog player.
android.content.res.Resources$NotFoundException: File res/raw/empty.wav from drawable resource ID #0x2120000
at android.content.res.ResourcesImpl.openRawResourceFd(ResourcesImpl.java:316)
at android.content.res.Resources.openRawResourceFd(Resources.java:1293)
at android.media.MediaPlayer.create(MediaPlayer.java:967)
at android.media.MediaPlayer.create(MediaPlayer.java:950)
at org.chromium.media.MediaServerCrashListener.startListening(PG:13)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
at android.content.res.AssetManager.openNonAssetFdNative(Native Method)
at android.content.res.AssetManager.openNonAssetFd(AssetManager.java:487)
at android.content.res.ResourcesImpl.openRawResourceFd(ResourcesImpl.java:314)
at android.content.res.Resources.openRawResourceFd(Resources.java:1293)
at android.media.MediaPlayer.create(MediaPlayer.java:967)
at android.media.MediaPlayer.create(MediaPlayer.java:950)
at org.chromium.media.MediaServerCrashListener.startListening(PG:13)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
2019-04-17 18:34:45.168 9326-9326/iakta.it.webviewtest E/cr_crMediaCrashListener: Unable to create watchdog player, treating it as server crash.
2019-04-17 18:34:45.197 9326-9326/iakta.it.webviewtest W/MediaPlayer: Couldn't open https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8: java.io.FileNotFoundException: No content provider: https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_4x3/bipbop_4x3_variant.m3u8
2019-04-17 18:34:45.198 9326-9326/iakta.it.webviewtest V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@dbda0d3): Cookies: null
2019-04-17 18:34:45.210 9326-9417/iakta.it.webviewtest V/MediaHTTPService: makeHTTPConnection: CookieManager created: java.net.CookieManager@6eff410
2019-04-17 18:34:45.210 9326-9417/iakta.it.webviewtest V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@dbda0d3): cookieHandler: java.net.CookieManager@6eff410 Cookies: null
2019-04-17 18:34:46.987 9326-9343/iakta.it.webviewtest I/zygote64: Waiting for a blocking GC ProfileSaver
关于如何将视频加载到WebView中的任何想法? 非常感谢。
答案 0 :(得分:1)
启用appcache后,它对我有用。
testWebView.settings.setAppCacheEnabled(true)
testWebView.loadUrl("https://developer.apple.com/streaming/examples/basic-stream-osx-ios4-3.html")