androidgoogle-cardboard

How do I build a Virtual Reality videoplayer without Unity?


I want to build a Virtual Reality video player without using Unity. Is there a way to use the mediaplayer?


Solution

  • You will need this:

    https://github.com/Rajawali/RajawaliVR

    It works well with media player and local files, but if you want to stream video, media player is not a reliable option.

    here is my example:

    PlayerActivity

    public class PlayerActivity extends RajawaliVRActivity {
    private RajawaliVRExampleRenderer mRenderer;
    private MediaPlayer mediaPlayer;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        mRenderer = new RajawaliVRExampleRenderer(PlayerActivity.this);
        setRenderer(mRenderer);
        setConvertTapIntoTrigger(true);
    
    }
    
    /**
     * Called when the Cardboard trigger is pulled.
     */
    @Override
    public void onCardboardTrigger() {
        RajLog.i("onCardboardTrigger");
    }
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    }
    

    And you need a renderer class (I'm sure you can modify MediaPlayer to suit your needs)

     public class RajawaliVRExampleRenderer extends RajawaliVRRenderer {
    //"https://ia700401.us.archive.org/19/items/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"
    Context mContext;
    
    private MediaPlayer mMediaPlayer;
    private StreamingTexture mVideoTexture;
    AppController appController;
    boolean playing=false;
    public RajawaliVRExampleRenderer(Context context) {
        super(context);
        mContext = context;
    }
    
    @Override
    public void initScene() {
    
        appController=(AppController)getContext().getApplicationContext();
        //mMediaPlayer = MediaPlayer.create(getContext(),R.raw.video1);
        mMediaPlayer=new MediaPlayer();
    
        try {
            mMediaPlayer.setAudioStreamType(AudioManager.USE_DEFAULT_STREAM_TYPE);
            mMediaPlayer.setDataSource("*URL*");
    
            mMediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        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.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.v("grebuf", "buffer (%):" + percent);
                if (percent >= 20) {
                    if (!playing) {
                        //mMediaPlayer.start();
                        playing = true;
                    }
                }
            }
        });
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.v("grega", "media start");
                mp.start();
            }
        });
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d("grebuf",what+" "+extra);
                return false;
            }
        });
    
    }
    
    @Override
    public 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();
    }
     }