我无法让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
答案 0 :(得分:0)
我能够通过使用AssetFileDescriptor而不是Uri
来实现这一点最终解决我问题的方法是执行以下操作:
AssetFileDescriptor afd = expansionFile.getAssetFileDescriptor(mFileName + ".m4v");
FileDescriptor fd = afd.getFileDescriptor();
long offset = afd.getStartOffset();
long length = afd.getLength();
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(fd,offset, length);
mMediaPlayer.prepare();
...
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
之前我尝试过这种方法,但是没有偏移和长度参数,但这没有用。你上面看到的是终于让它发挥作用。
之前我的错误方法是使用“ZipFileContentProvider”,如下所示:
public final static String AUTHORITY = "com.example.app.provider.ZipFileContentProvider";
public final static Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY);
String videoFilePath = CONTENT_URI + "/" + mFileName + ".m4v";
Uri zipVideo = Uri.parse(videoFilePath);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, zipVideo);
mMediaPlayer.setDisplay(mHolder);
mMediaPlayer.prepare();
然而,正如在原始问题中提到的那样,这只适用于某些特定情况,这远非理想。