Google vr sdk的使用

效果图

Google vr sdk的使用_第1张图片


导入sdk前先修改相应参数

Google vr sdk的使用_第2张图片

Google vr sdk的使用_第3张图片

引入

Google vr sdk的使用_第4张图片

依赖

Google vr sdk的使用_第5张图片

VRPlayActivity

public class VrActivity extends Activity{
   
@InjectView(R.id.video_view)
    VrVideoView
videoView;
   
@InjectView(R.id.seek_bar)
    SeekBar
seekBar;
    private static final
String STATE_PROGRESS_TIME = "progressTime";
    private static final
String STATE_IS_PAUSED = "isPaused";
    private static final
String STATE_VIDEO_DURATION = "videoDuration";
    private int
loadVideoStatus = LOAD_VIDEO_STATUS_UNKNOWN;
    public static final int
LOAD_VIDEO_STATUS_UNKNOWN = 0;
    public static final int
LOAD_VIDEO_STATUS_SUCCESS = 1;
    public static final int
LOAD_VIDEO_STATUS_ERROR = 2;
    private boolean
isPaused = false;
    private
VideoLoaderTask backgroundVideoLoaderTask;
    private
VrVideoView.Options videoOptions = new VrVideoView.Options();
    private
Uri fileUri;

   
@Override
   
protected void onCreate(BundlesavedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.vr_activity);
       
ButterKnife.inject(this);
       
seekBar.setOnSeekBarChangeListener(new SeekBarListener());
       
videoView.setEventListener(new ActivityEventListener());
       
loadVideoStatus = LOAD_VIDEO_STATUS_UNKNOWN;
       
handleIntent(getIntent());
   
}

   
private class ActivityEventListener extendsVrVideoEventListener {

       
@Override
       
public void onLoadSuccess() {
           
seekBar.setMax((int) videoView.getDuration());
       
}

       
@Override
       
public void onClick() {
            togglePause()
;
       
}

       
@Override
       
public void onNewFrame() {
            
seekBar.setProgress((int) videoView.getCurrentPosition());
       
}

    }

   
@Override
   
public void onRestoreInstanceState(BundlesavedInstanceState) {
       
super.onRestoreInstanceState(savedInstanceState);

        long
progressTime = savedInstanceState.getLong(STATE_PROGRESS_TIME);
       
videoView.seekTo(progressTime);
       
seekBar.setMax((int) savedInstanceState.getLong(STATE_VIDEO_DURATION));
       
seekBar.setProgress((int) progressTime);

       
isPaused = savedInstanceState.getBoolean(STATE_IS_PAUSED);
        if
(isPaused) {
           
videoView.pauseVideo();
       
}
    }

   
private class SeekBarListener implementsSeekBar.OnSeekBarChangeListener {
       
@Override
       
public void onProgressChanged(SeekBarseekBar, int progress, boolean fromUser){
           
if (fromUser){
               
videoView.seekTo(progress);
           
} // elsethis was from the ActivityEventHandler.onNewFrame()'s seekBar.setProgressupdate.
       
}

       
@Override
       
public void onStartTrackingTouch(SeekBarseekBar) {
        }

       
@Override
       
public void onStopTrackingTouch(SeekBarseekBar) {
        }
    }

   
private void handleIntent(Intent intent){
       
if (Intent.ACTION_VIEW.equals(intent.getAction())) {

           
fileUri =intent.getData();
            if
(fileUri== null) {
                Log.w(
"TAG", "Nodata uri specified. Use \"-d /path/filename\".");
           
} else{
                Log.i(
"TAG", "Usingfile " + fileUri.toString());
           
}

           
videoOptions.inputFormat = intent.getIntExtra("inputFormat", VrVideoView.Options.FORMAT_DEFAULT);
           
videoOptions.inputType = intent.getIntExtra("inputType", VrVideoView.Options.TYPE_MONO);
       
} else{
            Log.i(
"TAG", "Intentis not ACTION_VIEW. Using the default video.");
           
fileUri = null;
       
}
       
if (backgroundVideoLoaderTask != null) {
           
// Cancel any task from a previous intent sent to thisactivity.
            
backgroundVideoLoaderTask.cancel(true);
       
}
       
backgroundVideoLoaderTask = new VideoLoaderTask();
       
backgroundVideoLoaderTask.execute(Pair.create(fileUri, videoOptions));
   
}

   
@Override
   
protected void onPause() {
       
super.onPause();
       
videoView.pauseRendering();
       
isPaused = true;
   
}

   
@Override
   
protected void onResume() {
       
super.onResume();
       
videoView.resumeRendering();
   
}

   
@Override
   
protected void onDestroy() {
       
videoView.shutdown();
        super
.onDestroy();
   
}

   
private void togglePause() {
       
if (isPaused) {
           
videoView.playVideo();
       
} else{
           
videoView.pauseVideo();
       
}
       
isPaused = !isPaused;
   
}

   
class VideoLoaderTaskextends AsyncTask, VrVideoView.Options>, Void, Boolean> {
       
@Override
       
protected BooleandoInBackground(Pair, VrVideoView.Options>...fileInformation) {

           
// No intent was specified, so we default to playing thelocal stereo-over-under video.
           
final VrVideoView.Optionsoptions = new VrVideoView.Options();
           
/**设置视频的播放焦点*/
           
options.inputType =VrVideoView.Options.TYPE_MONO;
           
Intent intent = getIntent();
            final
String uri = intent.getStringExtra("uri");
           
Log.i("TAG", uri + "uri");
            if
(uri != null) {
                runOnUiThread(
new Runnable() {
                   
@Override
                   
public void run() {
                       
try {
                           
videoView.loadVideo(Uri.parse(uri), options);
                       
} catch (IOException e) {
                           e.printStackTrace()
;
                       
}
                    }
                })
;

//                   videoView.loadVideoFromAsset("congo.mp4", options);
           
}


           
return true;
       
}
    }

 xml布局

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical" android:layout_width="match_parent"
   
android:layout_height="match_parent">
           
android:id="@+id/video_view"
       
android:layout_width="match_parent"
       
android:layout_height="250dip"/>

   

   
        android:id="@+id/seek_bar"
       
style="?android:attr/progressBarStyleHorizontal"
       
android:layout_height="32dp"
       
android:layout_width="match_parent"
       
/>



你可能感兴趣的:(移动开发002)