我目前正在开发一款Android应用程序,其中包括一个视频播放器,它使用Android的VideoView通过HTTP加载mp4视频文件。 它适用于安装了Android 5.1.1的Nexus 4以及安装了Android 4.4的平板电脑。
但是,使用Android 6.0后,视频流会在几分钟后意外终止(在Nexus 5和仿真器上测试)。 正如您在日志输出中看到的那样,流将播放约5分钟,直到发生java.net.ProtocolException ,另外1分钟播放缓冲区为空。
10-16 11:08:21.030 4657-4657/...app D/video: start
10-16 11:08:21.060 4657-4657/...app W/MediaPlayer: Couldn't open file on client side; trying server side: java.io.FileNotFoundException: No content provider: http://someurl...mp4
10-16 11:08:29.330 4657-4657/...app D/MediaPlayer: getMetadata
10-16 11:08:29.360 4657-4657/...app D/video: prepared
10-16 11:08:29.470 4657-4671/...app W/MediaPlayer: info/warning (3, 0)
10-16 11:09:19.930 4657-4664/...app W/art: Suspending all threads took: 10ms
10-16 11:14:32.550 4657-4664/...app W/art: Suspending all threads took: 10ms
10-16 11:23:46.940 4657-4671/...app W/MediaHTTPConnection: readAt 121022400 / 22592 => java.net.ProtocolException: unexpected end of stream
10-16 11:24:40.780 4657-4669/...app E/MediaPlayer: error (1, -1004)
10-16 11:24:40.780 4657-4657/...app E/MediaPlayer: Error (1,-1004)
10-16 11:24:40.780 4657-4657/...app D/VideoView: Error: 1,-1004
10-16 11:24:40.780 4657-6289/...app E/MediaPlayer: error (1, -1004)
10-16 11:24:40.790 4657-4713/...app E/MediaPlayer: error (1, -1004)
10-16 11:24:40.830 4657-4657/...app E/MediaPlayer: Error (1,-1004)
10-16 11:24:40.830 4657-4657/...app D/VideoView: Error: 1,-1004
10-16 11:24:40.840 4657-4657/...app E/MediaPlayer: Error (1,-1004)
10-16 11:24:40.840 4657-4657/...app D/VideoView: Error: 1,-1004
10-16 11:24:40.860 4657-4697/...app W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 11:24:40.860 4657-4697/...app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe0d7e740, error=EGL_SUCCESS
10-16 11:24:40.880 4657-6289/...app E/MediaPlayer: error (1, -1004)
10-16 11:24:40.880 4657-4713/...app E/MediaPlayer: error (1, -1004)
10-16 11:24:40.890 4657-4697/...app W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 11:24:40.890 4657-4697/...app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe11fbd80, error=EGL_SUCCESS
10-16 11:24:40.920 4657-4697/...app W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 11:24:40.920 4657-4697/...app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xed7939e0, error=EGL_SUCCESS
10-16 11:24:40.950 4657-4657/...app E/MediaPlayer: Error (1,-1004)
10-16 11:24:40.950 4657-4657/...app D/VideoView: Error: 1,-1004
10-16 11:24:40.950 4657-4657/...app E/MediaPlayer: Error (1,-1004)
10-16 11:24:40.950 4657-4657/...app D/VideoView: Error: 1,-1004
10-16 11:24:41.010 4657-4697/...app W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 11:24:41.010 4657-4697/...app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe0ed12e0, error=EGL_SUCCESS
10-16 11:24:41.030 4657-4697/...app W/EGL_emulation: eglSurfaceAttrib not implemented
10-16 11:24:41.030 4657-4697/...app W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe0ed1300, error=EGL_SUCCESS
添加唤醒和wifi锁似乎并没有阻止这种行为。
以下是视频播放器片段的布局配置:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VideoActivity"
android:keepScreenOn="true">
<VideoView
android:id="@+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
...
部分代码:
videoView = (VideoView) v.findViewById(R.id.video);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setVideoURI(videoUrl);
videoView.seekTo(0);
videoView.start();