首页 > 代码库 > VR开发基础—VR视频

VR开发基础—VR视频

1、导入谷歌官方提供的库:

commonwidget、common、panowidget(全景图)、videowidget(视频)
或者添加依赖:
dependencies {
compile project(‘:libraries-common‘)
compile project(‘:libraries-commonwidget‘)
compile project(‘:libraries-videowidget)
}
版本要求:
<uses-sdkandroid:minSdkVersion="19"android:targetSdkVersion="22"/>
2、配置清单文件:
<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/>
<applicationandroid:label="SimpleVrPanoramaActivity"
    android:largeHeap="true"
    android:theme="@android:style/Theme.Holo.Light">
    <activityandroid:name=".SimpleVrVedioActivity">
        <intent-filter>
            <actionandroid:name="android.intent.action.MAIN"/>
            <categoryandroid:name="android.intent.category.LAUNCHER"/>
            <categoryandroid:name="com.google.intent.category.CARDBOARD"/>
        </intent-filter>
    </activity>
</application>
3、布局加载 全景资源(图片)控件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF000"
    android:orientation="vertical">
    <com.google.vr.sdk.widgets.video.VrVideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="500dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="horizontal">
        <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>
        <TextView
            android:id="@+id/status_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:padding="8dp"
            android:text="00:00"/>
    </LinearLayout>
</FrameLayout>
4、初始化全景视频
privatevoid initVideoView() {
    videoView =(VrVideoView) findViewById(R.id.video_view);
    //只能在主线程里面加载资产目录
    VrVideoView.Optionsoption =newVrVideoView.Options();
//输入格式
    option.inputFormat =VrVideoView.Options.FORMAT_DEFAULT;
//声音模式
    option.inputType =VrVideoView.Options.TYPE_MONO;
    try {
        videoView.loadVideoFromAsset("congo.mp4", option);
// String path= Environment.getExternalStorageDirectory()+"/testRoom1_1080Stereo.mp4";
// String path= Environment.getExternalStorageDirectory()+"/testRoom1_1920Mono.mp4";
// Uri uri= Uri.parse("file:///"+path);
// videoView.loadVideo(uri,option);
        isPause=true;
    } catch(Exception e) {
        e.printStackTrace();
        isPause=false;
    }
    videoView.setEventListener(newVrVideoEventListener() {
        @Override
        publicvoid onl oadSuccess() {
            super.onLoadSuccess();
            System.out.println("onLoadSuccess");
        }
//点击
        @Override
        publicvoid onClick() {
            super.onClick();
            toggle();
        }
        @Override
        publicvoid onl oadError(String errorMessage) {
            super.onLoadError(errorMessage);
            System.out.println("onLoadError "+ errorMessage);
        }
        @Override
        publicvoid onCompletion() {
            super.onCompletion();
            System.out.println("onCompletion ");
        }
        @Override
        publicvoid onNewFrame() {
            super.onNewFrame();
            System.out.println("onNewFrame ");
            refreshStatusBar();
        }
    });
}
privateboolean isPause =false;
privatevoid toggle() {
    if(isPause) {
        isPause =false;
        videoView.playVideo();
    } else {
        isPause =true;
        videoView.pauseVideo();
    }
    refreshStatusBar();
}
5、初始化SeekBar进度条
//初始化拖动控件
privatevoid initSeekBar() {
    seekbar =(SeekBar) findViewById(R.id.seek_bar);
    seekbar.setMax(100);
    seekbar.setProgress(0);
    statusText =(TextView) findViewById(R.id.status_text);
//添加事件
    seekbar.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {
        @Override
        publicvoid onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
            System.out.println("onProgressChanged fromUser="+ fromUser);
            if(fromUser) {
//指定位置继续播放
                videoView.seekTo(progress);
                refreshStatusBar();
            }
        }
        @Override
        publicvoid onStartTrackingTouch(SeekBar seekBar) {
            System.out.println("SeekBar onStartTrackingTouch");
        }
        @Override
        publicvoid onStopTrackingTouch(SeekBar seekBar) {
            System.out.println("SeekBar onStopTrackingTouch");
        }
    });
}
privatevoid refreshStatusBar() {
    StringBuilder status =newStringBuilder();
    status.append(videoView.getCurrentPosition()/1000f);
    status.append(" / ");
    status.append(videoView.getDuration()/1000f);
    status.append("s");
    statusText.setText(status);
}
6、关联activity
@Override
protectedvoid onPause() {
    super.onPause();
    videoView.pauseRendering();//停止显示
}
@Override
protectedvoid onResume() {
    super.onResume();
    videoView.resumeRendering();//继续显示
    refreshStatusBar();
}
@Override
protectedvoid onDestroy() {
    videoView.shutdown();//停止播放
    super.onDestroy();

}

 

VR开发基础—VR视频