Reprint please indicate the source: blog.csdn.net/linglongxin… This post is from DylanAndroid’s blog.


[Android development VR combat] two. Play 360° panoramic video

Virtual Reality. Virtual reality technology is a computer simulation system that can create and experience the virtual world. It uses the computer to generate a simulation environment, which is a multi-source information fusion interactive THREE-DIMENSIONAL dynamic view and system simulation of entity behavior. Users are immersed in the environment. So, how to develop VR function APP in Android? We use the open source SDK provided by Google to achieve a 360° panoramic video function


Build. Gradle with Google VR SDK dependencies

     compile 'com. Google. Vr: SDK - videowidget: 1.10.0'Copy the code

Note the minimum SDK supported

  minSdkVersion 19
  targetSdkVersion 25Copy the code

3. Interface layout file

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.bluemobi.dylan.vrdevelopvideo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android development VR360-degree panoramic video" />

    <com.google.vr.sdk.widgets.video.VrVideoView
        android:id="@+id/vr_video_view"
        android:layout_width="match_parent"
        android:layout_height="250dp"></com.google.vr.sdk.widgets.video.VrVideoView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/play_toggle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:paddingStart="0dp"
            android:src="@drawable/pause" />

        <SeekBar
            android:id="@+id/seek_bar"
            style="? android:attr/progressBarStyleHorizontal"
            android:layout_width="0dp"
            android:layout_height="32dp"
            android:layout_weight="8" />

        <ImageButton
            android:id="@+id/volume_toggle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:paddingStart="0dp"
            android:paddingTop="4dp"
            android:src="@drawable/volume_on" />
    </LinearLayout>
</LinearLayout>Copy the code

Load 360° panoramic video

    /** * load 360-degree video */
    private void load360Video(a) {
        vr_video_view = (VrVideoView) findViewById(R.id.vr_video_view);
        seek_bar = (SeekBar) findViewById(R.id.seek_bar);
        volume_toggle = (ImageButton) findViewById(R.id.volume_toggle);
        play_toggle = (ImageButton) findViewById(R.id.play_toggle);

        /** set load Settings **/
        VrVideoView.Options options = new VrVideoView.Options();
        options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER;
        /** * set load listener */
        vr_video_view.setEventListener(new VrVideoEventListener() {
            /** * The video playback is complete callback */
            @Override
            public void onCompletion(a) {
                super.onCompletion();
                /** jump to start replaying **/
                vr_video_view.seekTo(0);
                setIsPlay(false);
                Log.d(TAG, "onCompletion()");
            }

            /** * Loads the callback for each frame of video */
            @Override
            public void onNewFrame(a) {
                super.onNewFrame();
                seek_bar.setProgress((int) vr_video_view.getCurrentPosition());
                Log.d(TAG, "onNewFrame()");
            }

            /** * Click the VR video callback */
            @Override
            public void onClick(a) {
                super.onClick();
                Log.d(TAG, "onClick()");
            }

            /** * Failed to load VR video callback *@param errorMessage
             */
            @Override
            public void onLoadError(String errorMessage) {
                super.onLoadError(errorMessage);
                Log.d(TAG, "onLoadError()->errorMessage=" + errorMessage);
            }

            /** * Successfully loaded VR video callback */
            @Override
            public void onLoadSuccess(a) {
                super.onLoadSuccess();
                /** Set callback **/ after loading successfully
                seek_bar.setMax((int) vr_video_view.getDuration());
                Log.d(TAG, "onNewFrame()");
            }

            /** * Display mode change callback * 1. Default * 2. Full screen mode * 3@param* / newDisplayMode mode
            @Override
            public void onDisplayModeChanged(int newDisplayMode) {
                super.onDisplayModeChanged(newDisplayMode);
                Log.d(TAG, "onLoadError()->newDisplayMode="+ newDisplayMode); }});try {
            /** Load VR video **/
            vr_video_view.loadVideoFromAsset("congo.mp4", options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        /** Set the sound button and click listen **/
        volume_toggle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setIsMuted(!isMuted);
            }
        });
        /** Set the pause button to listen **/
        play_toggle.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                setIsPlay(!isPlay);
            }
        });

        /** Set progress bar drag listener **/
        seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /** * Progress bar drag to change listener *@paramSeekBar Drag bar *@param* the progress schedule@paramWhether fromUser is manually operated */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    /** Adjust video progress **/vr_video_view.seekTo(progress); }}@Override
            public void onStartTrackingTouch(SeekBar seekBar) {}@Override
            public void onStopTrackingTouch(SeekBar seekBar) {}}); }/** * Sets the sound switch **@paramIsMuted switch * /
    private void setIsMuted(boolean isMuted) {
        this.isMuted = isMuted;
        volume_toggle.setImageResource(isMuted ? R.drawable.volume_off : R.drawable.volume_on);
        vr_video_view.setVolume(isMuted ? 0.0 f : 1.0 f);
    }

    /** * Set playback pause **@paramIsPlay play pause */
    private void setIsPlay(boolean isPlay) {
        this.isPlay = isPlay; play_toggle.setImageResource(isPlay ? R.drawable.pause: R.drawable.play );if(isPlay){
            vr_video_view.playVideo();
        }else{ vr_video_view.pauseVideo(); }}Copy the code

Five.GitHub