如何使用自定义GLSurfaceView播放视频?

时间:2013-04-06 07:22:18

标签: android xml glsurfaceview

在我的应用程序中我希望在目标图像上播放3Dvideo,我已经开始使用c ++进行摄像,并且能够使用自定义视图在目标图像上设置3D图像,现在我的要求是在目标图像上播放3D视频代替3D图像,我有尝试使用自定义GLSurfaceView播放视频。下面是我的代码但是没有调用onSurfaceCreated方法。

public class GLPreview extends GLSurfaceView implements
    OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
    OnVideoSizeChangedListener, SurfaceHolder.Callback,
    GLSurfaceView.Renderer{

    private static final String TAG = "MediaPlayerDemo";
    private int mVideoWidth;
    private int mVideoHeight;
    private MediaPlayer mMediaPlayer;
    private GLSurfaceView mPreview;
    private SurfaceHolder holder;
    private String path;
    private Bundle extras;
    private static final String MEDIA = "media";
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
    private boolean mIsVideoSizeKnown = false;
    private boolean mIsVideoReadyToBePlayed = false;
    Context context2;

    public GLPreview(Context context, AttributeSet attrs) {
        super(context, attrs);
        // this.setZOrderMediaOverlay(true);
        Log.d(TAG, "GLPreview: OnPause");
        this.context2 = context;

        mPreview = (GLSurfaceView) findViewById(R.id.surface);
        // holder = mPreview.getHolder();
        setBackgroundResource(R.drawable.vuforiasizzlereel);
    }

    public void playVideo() {
        doCleanUp();
        try {

            // Create a new media player and set the listeners
            mMediaPlayer = new MediaPlayer();
                mMediaPlayer.setDataSource(path);           
            mMediaPlayer.setDisplay(holder);
            mMediaPlayer.prepare();
            mMediaPlayer.setOnBufferingUpdateListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnVideoSizeChangedListener(this);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }
    }
    public void onPause() {
        super.onPause();
        Log.d(TAG, "GLPreview: OnPause");
    }

    public void onResume() {
        // this.setZOrderMediaOverlay(true);
        super.onResume();
        Log.d(TAG, "GLPreview: OnResume");
    }
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(TAG, "GLPreview: surfaceCreated");
        super.surfaceCreated(holder);
    }
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        Log.d(TAG, String.format(
                "GLPreview: surfaceChanged, format=%d, w=%d, h=%d", format, w,
                h));
        super.surfaceChanged(holder, format, w, h);
    }
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(TAG, "GLPreview: surfaceDestroyed");
        super.surfaceDestroyed(holder);
    }
    protected void onAttachedToWindow() {
        Log.d(TAG, "GLPreview: onAttachedToWindow");
        super.onAttachedToWindow();
    }
    protected void onDetachedFromWindow() {
        Log.d(TAG, "GLPreview: onDetachedFromWindow");
        super.onDetachedFromWindow();
    }
    protected void onWindowVisibilityChanged(int vis) {
        String newVisibility;
        switch (vis) {
        case View.GONE:
            newVisibility = "GONE";
            break;
        case View.INVISIBLE:
            newVisibility = "INVISIBLE";
            break;
        case View.VISIBLE:
            newVisibility = "VISIBLE";
            break;
        default:
            newVisibility = String.format("Unknown constant %d", vis);
        }

        Log.d(TAG, String.format("GLPreview: onWindowVisibilityChanged -> %s",
                newVisibility));
        super.onWindowVisibilityChanged(vis);
    }
    private void doCleanUp() {
        mVideoWidth = 0;
        mVideoHeight = 0;
        mIsVideoReadyToBePlayed = false;
        mIsVideoSizeKnown = false;
    }
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
        Log.v(TAG, "onVideoSizeChanged called");
        if (width == 0 || height == 0) {
            Log.e(TAG, "invalid video width(" + width + ") or height(" + height
                    + ")");
            return;
        }
        mIsVideoSizeKnown = true;
        mVideoWidth = width;
        mVideoHeight = height;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }
    public void onPrepared(MediaPlayer mediaplayer) {
        Log.d(TAG, "onPrepared called");
        mIsVideoReadyToBePlayed = true;
        if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
            startVideoPlayback();
        }
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        // TODO Auto-generated method stub
    }
    private void startVideoPlayback() {
        Log.v(TAG, "startVideoPlayback");
        // holder.setFixedSize(mVideoWidth, mVideoHeight);
        mMediaPlayer.start();
    }
    @Override
    public void onDrawFrame(GL10 gl) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
    }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // TODO Auto-generated method stub
        Log.d(TAG, "GLPreview : onSurfaceCreated");
        playVideo();
    }
}

致电自定义GLsurfaceview:

GLPreview productView = new GLPreview(CloudReco.this, null); 

我不知道我在哪里失踪。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

在这种情况下,您不应该从GLPreview实现SurfaceHolder.Callback,因为GLSurfaceView已经将回调设置为surfaceholder并调用renderer.surfacecreated。所以你应该只实现GLSurfaceView.Renderer方法 - ondrawframe,onsurfacecreated(GL10 gl,EGLConfig config)。请参阅GLsurfaceView源代码。 GLSurfaceView已经实现了surfaceCreated(SurfaceHolder holder);并调用您的渲染器