Search code examples
google-cardboardrajawalivr

Making a VR video player using Google Cardboard


How to make a simple VR video player with Google CardBoard Android SDK ? I am a new man in this area. I am trying to change a program in github to play video for cardboard now !


Solution

  • I used Rajawali Library along with Google Cardboard Sdk and Modified initscene() in this way to get this work.

    public class VideoRenderer extends RajawaliCardboardRenderer {
    
    Context mContext;
    
    private MediaPlayer mMediaPlayer;
    private StreamingTexture mVideoTexture;
    
    public VideoRenderer(Context context) {
        super(context);
        mContext = context;
    }
    
    @Override
    protected void initScene() {
    
        mMediaPlayer = MediaPlayer.create(getContext(),
                R.raw.video);
        mMediaPlayer.setLooping(true);
    
        mVideoTexture = new StreamingTexture("sintelTrailer", mMediaPlayer);
        Material material = new Material();
        material.setColorInfluence(0);
        try {
            material.addTexture(mVideoTexture);
        } catch (ATexture.TextureException e) {
            e.printStackTrace();
        }
    
        Sphere sphere = new Sphere(50, 64, 32);
        sphere.setScaleX(-1);
        sphere.setMaterial(material);
    
        getCurrentScene().addChild(sphere);
    
        getCurrentCamera().setPosition(Vector3.ZERO);
    
        getCurrentCamera().setFieldOfView(75);
    
        mMediaPlayer.start();
    
    }
    
    @Override
    protected void onRender(long ellapsedRealtime, double deltaTime) {
        super.onRender(ellapsedRealtime, deltaTime);
        mVideoTexture.update();
    }
    
    @Override
    public void onPause() {
        super.onPause();
        if (mMediaPlayer != null)
            mMediaPlayer.pause();
    }
    
    @Override
    public void onResume() {
        super.onResume();
        if (mMediaPlayer != null)
            mMediaPlayer.start();
    }
    
    @Override
    public void onRenderSurfaceDestroyed(SurfaceTexture surfaceTexture) {
        super.onRenderSurfaceDestroyed(surfaceTexture);
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }
     }