我在让 MediaPlayer 在 android 4.3(以及更低版本)上可靠地播放视频时遇到了一些麻烦。大多数代码只是标准样板文件(为了完整性而包含在内)。
我正在使用 APK 扩展库,而我的 obb 只是 som 缩略图和一些 m4v 视频文件的 zip,以及用于描述视频的 XML 文件。
我认为存在时间问题,因为该错误似乎仅在播放小文件(<30秒或在我的情况下<3Mb)时发生。mMediaPlayer.prepare()
在以下代码中运行时会发生这种情况:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.activity_play_video);
Intent intent = getIntent();
mFileName = intent.getStringExtra("Video"); //which video should we play
...
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}
public void playVideo() {
String videoFilePath = Const.CONTENT_URI + "/" + mFileName + ".m4v";
Uri zipVideo = Uri.parse(videoFilePath);
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(getApplicationContext(), zipVideo);
mMediaPlayer.setDisplay(mHolder);
mMediaPlayer.prepare();
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnErrorListener (new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.d("ERROR", "what: " + what + " extra: " + extra);
return false;
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
}
...
}
@Override
public void onPrepared(MediaPlayer mp) {
Log.d("playback " , "onPrepared");
start();
}
我一直在调查这个问题,似乎setDataSource(...)
没有设置mMediaPlayer
之前的正确状态prepare()
被调用。
使这个问题更有趣的是,当播放较大的文件时,它可以完美地工作。
编辑: 这里是 logcat 的输出。
11-01 13:19:12.483: E/MediaPlayer(26369): prepareAsync called in state 1
11-01 13:19:12.483: W/System.err(26369): java.lang.IllegalStateException
11-01 13:19:12.483: W/System.err(26369): at android.media.MediaPlayer.prepare(Native Method)
11-01 13:19:12.483: W/System.err(26369): at com.skilessons4u.lite.PlayVideoActivity.prepareVideo(PlayVideoActivity.java:126)
11-01 13:19:12.483: W/System.err(26369): at com.skilessons4u.lite.PlayVideoActivity.surfaceCreated(PlayVideoActivity.java:100)
11-01 13:19:12.483: W/System.err(26369): at android.view.SurfaceView.updateWindow(SurfaceView.java:571)
11-01 13:19:12.483: W/System.err(26369): at android.view.SurfaceView.access$000(SurfaceView.java:86)
11-01 13:19:12.483: W/System.err(26369): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:175)
11-01 13:19:12.483: W/System.err(26369): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:833)
11-01 13:19:12.483: W/System.err(26369): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1860)
11-01 13:19:12.483: W/System.err(26369): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
11-01 13:19:12.483: W/System.err(26369): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
11-01 13:19:12.483: W/System.err(26369): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
11-01 13:19:12.483: W/System.err(26369): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
11-01 13:19:12.483: W/System.err(26369): at android.view.Choreographer.doFrame(Choreographer.java:532)
11-01 13:19:12.483: W/System.err(26369): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
11-01 13:19:12.483: W/System.err(26369): at android.os.Handler.handleCallback(Handler.java:730)
11-01 13:19:12.483: W/System.err(26369): at android.os.Handler.dispatchMessage(Handler.java:92)
11-01 13:19:12.483: W/System.err(26369): at android.os.Looper.loop(Looper.java:137)
11-01 13:19:12.483: W/System.err(26369): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-01 13:19:12.483: W/System.err(26369): at java.lang.reflect.Method.invokeNative(Native Method)
11-01 13:19:12.483: W/System.err(26369): at java.lang.reflect.Method.invoke(Method.java:525)
11-01 13:19:12.483: W/System.err(26369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-01 13:19:12.483: W/System.err(26369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-01 13:19:12.483: W/System.err(26369): at dalvik.system.NativeStart.main(Native Method)
为了完整起见,这里是我在脚本中运行以生成 obb 文件的命令。缩略图显示得很好,很难。
zip -rn .m4v:.png $ZIPFILE $XMLFILE $VIDEOSDIR $THUMBDIR
zipalign 4 $ZIPFILE $OBBFILE