VR开发基础—VR视频

1、导入谷歌官方提供的库:
 
   
commonwidget、common、panowidget(全景图)、videowidget(视频)
或者添加依赖:
 
   
dependencies {
compile project(':libraries-common')
compile project(':libraries-commonwidget')
compile project(':libraries-videowidget')
}
版本要求:
 
   
android:minSdkVersion="19" android:targetSdkVersion="22" />
2、配置清单文件:
 
   
android:name="android.permission.INTERNET" />
android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:label="SimpleVrPanoramaActivity"
android:largeHeap="true"
android:theme="@android:style/Theme.Holo.Light">
android:name=".SimpleVrVedioActivity">
android:name="android.intent.action.MAIN" />
android:name="android.intent.category.LAUNCHER" />
android:name="com.google.intent.category.CARDBOARD" />
3、 布局加载 全景资源(图片)控件
 
   
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF000"
android:orientation="vertical">
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="500dp" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
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" />
4、初始化全景视频
 
   
private void initVideoView() {
videoView = (VrVideoView) findViewById(R.id.video_view);
    //只能在主线程里面加载资产目录
VrVideoView.Options option = new VrVideoView.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(new VrVideoEventListener() {
@Override
public void onLoadSuccess() {
super.onLoadSuccess();
System.out.println("onLoadSuccess");
}
//点击
@Override
public void onClick() {
super.onClick();
toggle();
}
@Override
public void onLoadError(String errorMessage) {
super.onLoadError(errorMessage);
System.out.println("onLoadError " + errorMessage);
}
@Override
public void onCompletion() {
super.onCompletion();
System.out.println("onCompletion ");
}
@Override
public void onNewFrame() {
super.onNewFrame();
System.out.println("onNewFrame ");
refreshStatusBar();
}
});
}
 
    
private boolean isPause = false;
private void toggle() {
if (isPause) {
isPause = false;
videoView.playVideo();
} else {
isPause = true;
videoView.pauseVideo();
}
refreshStatusBar();
}
5、初始化SeekBar进度条
 
   
//初始化拖动控件
private void initSeekBar() {
seekbar = (SeekBar) findViewById(R.id.seek_bar);
seekbar.setMax(100);
seekbar.setProgress(0);
statusText = (TextView) findViewById(R.id.status_text);
//添加事件
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
System.out.println("onProgressChanged fromUser=" + fromUser);
if (fromUser) {
//指定位置继续播放
videoView.seekTo(progress);
refreshStatusBar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
System.out.println("SeekBar onStartTrackingTouch");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
System.out.println("SeekBar onStopTrackingTouch");
}
});
}
 
    
private void refreshStatusBar() {
StringBuilder status = new StringBuilder();
status.append(videoView.getCurrentPosition() / 1000f);
status.append(" / ");
status.append(videoView.getDuration() / 1000f);
status.append("s");
statusText.setText(status);
}
6、关联activity
 
    
@Override
protected void onPause() {
super.onPause();
videoView.pauseRendering();//停止显示
}
@Override
protected void onResume() {
super.onResume();
videoView.resumeRendering();//继续显示
refreshStatusBar();
}
@Override
protected void onDestroy() {
videoView.shutdown();//停止播放
super.onDestroy();
 
}

你可能感兴趣的:(Android)