1

在我的应用程序中,我想在目标图像上播放 3Dvideo,我已经在 C++ 中启动了相机,并且能够使用自定义视图在目标图像上设置 3D 图像,现在我的要求是在目标图像上播放 3Dvideo 代替 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); 

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

4

1 回答 1

0

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

于 2014-10-13T19:31:16.590 回答