项目引入:
最新版本:https://github.com/googlevr/gvr-android-sdk/releases
implementation 'com.google.vr:sdk-panowidget:1.30.0'
implementation 'com.google.vr:sdk-videowidget:1.30.0'
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.vr.sdk.widgets.video.VrVideoView
android:id="@+id/vr_video"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</RelativeLayout>
代码:
public class VideoPlayerActivity extends AppCompatActivity {
private VrVideoView vr_video;
private SeekBar seekBar;
private String url = "http://xxx.mp4";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
vr_video = findViewById(R.id.vr_video);
seekBar = findViewById(R.id.seek_bar);
vr_video.setEventListener(new VrVideoEventListener() {
@Override
public void onLoadSuccess() {
super.onLoadSuccess();
seekBar.setMax((int) vr_video.getDuration());
}
@Override
public void onNewFrame() {
super.onNewFrame();
seekBar.setProgress((int) vr_video.getCurrentPosition());
}
});
VrVideoView.Options options = new VrVideoView.Options();
options.inputType = VrVideoView.Options.TYPE_MONO;
options.inputFormat = VrVideoView.Options.FORMAT_DEFAULT;
try {
vr_video.loadVideo(Uri.parse(url), options);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
vr_video.resumeRendering();
}
@Override
protected void onPause() {
super.onPause();
vr_video.pauseRendering();
}
@Override
protected void onDestroy() {
super.onDestroy();
vr_video.shutdown();
}
}
在github上面找到了一种更简单的方法,可以直接通过mediaplay播放VR
https://github.com/ashqal/MD360Player4Android
//引入
implementation 'com.github.ashqal:MD360Player4Android:2.5.0'
//布局
<android.opengl.GLSurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
mediaPlayer = MediaPlayer.create(this, R.raw.vr);
mVRLibrary = MDVRLibrary.with(this)
.displayMode(MDVRLibrary.DISPLAY_MODE_NORMAL)
.interactiveMode(MDVRLibrary.INTERACTIVE_MODE_MOTION)
.asVideo(surface -> {
mediaPlayer.setSurface(surface);
})
.build((GLSurfaceView) findViewById(R.id.surface_view));
mediaPlayer.start();