2013-04-06 46 views
1

在我的應用程序中,我想在目標圖像上播放3Dvideo,我已經開始在C++中使用相機,並且可以使用自定義視圖在目標圖像上設置3D圖像,現在我的要求是在目標圖像上播放3Dvideo以代替3D圖像,使用自定義GLSurfaceView播放視頻。下面是我的代碼,但沒有調用onSurfaceCreated方法。如何使用自定義GLSurfaceView播放視頻?

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); 

我不知道我在哪裏丟失。 在此先感謝。

+0

看看這個鏈接可能會得到任何想法http://stackoverflow.com/q/11431676/760489,http://sudarnimalan.blogspot.in/2012/06/android-play-video- on-top-of.html,http://www.makeuseof.com/tag/create-custom-video-player-website-vp-factory/ – Pratik 2013-04-06 08:50:29

回答

0

在這種情況下,您不應該從GLPreview實現SurfaceHolder.Callback,因爲GLSurfaceView已經將回調函數設置爲水線頭並調用renderer.surfacecreated。所以你應該只實現GLSurfaceView.Renderer方法 - ondrawframe,onsurfacecreated(GL10 gl,EGLConfig配置)。請參閱GLsurfaceView源代碼。 GLSurfaceView已經實現了SurfaceCreated(SurfaceHolder holder);並調用你的渲染器

相關問題