我已经为此苦苦挣扎了 2 天......按照这个答案:https ://stackoverflow.com/a/2006454/444324 - 提到可以通过更改 MediaPlayerDemo_Video 示例在 GLSurfaceView 中播放视频在 API 演示中:
您所要做的就是在 MediaPlayerDemo_Video.java 文件以及相应的布局文件 (mediaplayer_2.xml) 中将 SurfaceView 替换为 GLSurfaceView。
您还需要创建一个自定义 Renderer 类(实现 GLSurfaceView.Renderer 接口的类)并将其设置为您的 GLSurfaceView。
我尝试按照建议将 SurfaceView 替换为 GLSurfaceView ,也使用它,但它只是在启动时崩溃:
07-11 14:54:22.086: E/AndroidRuntime(12373): FATAL EXCEPTION: main
07-11 14:54:22.086: E/AndroidRuntime(12373): java.lang.NullPointerException
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.opengl.GLSurfaceView.surfaceCreated(GLSurfaceView.java:512)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.SurfaceView.updateWindow(SurfaceView.java:533)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.SurfaceView.access$000(SurfaceView.java:81)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:590)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1617)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.os.Looper.loop(Looper.java:137)
07-11 14:54:22.086: E/AndroidRuntime(12373): at android.app.ActivityThread.main(ActivityThread.java:4575)
07-11 14:54:22.086: E/AndroidRuntime(12373): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 14:54:22.086: E/AndroidRuntime(12373): at java.lang.reflect.Method.invoke(Method.java:511)
07-11 14:54:22.086: E/AndroidRuntime(12373): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-11 14:54:22.086: E/AndroidRuntime(12373): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-11 14:54:22.086: E/AndroidRuntime(12373): at dalvik.system.NativeStart.main(Native Method)
我知道我可以使用 VideoView 播放视频,或者只使用带有 MediaPlayer 的 SurfaceView,但我必须使用 GLSurfaceView,因为我需要在相机表面视图上播放此视频。
谢谢!
相关代码:我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.commonsware.android.camera.MyGLSurfaceView
android:id="@+id/surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
我的课:
public class MediaPlayerDemo_Video extends Activity implements
OnBufferingUpdateListener, OnCompletionListener,
OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
private MediaPlayer mMediaPlayer;
private MyGLSurfaceView mPreview;
private SurfaceHolder holder;
/**
*
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.mediaplayer_2);
mPreview = (MyGLSurfaceView) findViewById(R.id.surface);
holder = mPreview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
}
MyGLSurfaceView 类:
class MyGLSurfaceView extends android.opengl.GLSurfaceView {
public MyGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}