Search code examples
androidvideo-streaminggoogle-cardboardrajawalirajawalivr

How to use RajawaliVR or Rajawali to play a 360 Video


I am having a hard time to figure out how to use the Rajawali to play a 360 video. In order to achieve this, I tried every solution I could find in the Internet, but I failed.

Firstly, I used the RajawaliCardboard, and let the MainActivity extend from CardboardActivity. At the same time, in MyRenderer class, I let this class extend from the RajawaliCardboardRenderer class. In MyRenderer class, I overrided the initScene() function:

protected void initScene() {
    StreamingTexture mTexture = null;
    if (externalMemoryAvailable())
    {
        mVideoPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/testVideo.mp4";
        try{
            mPlayer = new MediaPlayer();
            mPlayer.setDataSource(mVideoPath);
        }catch(IllegalArgumentException e){
        e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            mPlayer.prepare();
        } catch (IOException t) {
            t.printStackTrace();
        }
        mTexture = new StreamingTexture("video", mPlayer);
    }
    Sphere sphere = createPhotoSphereWithTexture(mTexture);
    getCurrentScene().addChild(sphere);
    getCurrentCamera().setPosition(Vector3.ZERO);
    getCurrentCamera().setFieldOfView(75);
}

private  Sphere createPhotoSphereWithTexture(ATexture texture) {
    Material material = new Material();
    material.setColor(0);
    try {
        material.addTexture(texture);
    } catch (ATexture.TextureException e) {
        throw new RuntimeException(e);
    }
    Sphere sphere = new Sphere(50, 64, 32);
    sphere.setScaleX(-1);
    sphere.setMaterial(material);
    return sphere;
}

The program can run without any error, but the screen is black and with no image.
I want to ask what should I do to improve my program, and why I should do to play video by using Rajawali. Who can help me?


Solution

  • I got success to play video with Rajawali.

    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();
        }
        public void nextVideo(String nextVideoPath){
            try{
                mMediaPlayer.stop();
                mMediaPlayer.reset();
    
                mMediaPlayer.setDataSource(nextVideoPath);
                mMediaPlayer.prepare();
                mMediaPlayer.start();
    
          }catch (Exception e){
            e.printStackTrace();
          }
        }
    }