轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53924006
本文出自【DylanAndroid的博客】
【Android開發VR實戰】二.播放360°全景視頻
VR即Virtual Reality虛擬現實。虛擬現實技術是一種可以創建和體驗虛擬世界的計算機仿真系統它利用計算機生成一種模擬環境是一種多源信息融合的交互式的三維動態視景和實體行爲的系統仿真使用戶沉浸到該環境中。
那麼,如何在Android中去開發VR功能的APP呢?我們利用谷歌提供的開源SDK去實現一個360°全景視頻的功能
一.在build.gradle中引入谷歌VR的SDK依賴
compile 'com.google.vr:sdk-videowidget:1.10.0'
- 1
- 1
二.注意支持的最小SDK
minSdkVersion 19
targetSdkVersion 25
- 1
- 2
- 1
- 2
三.界面佈局文件
<?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開發VR360度全景視頻" />
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
四.加載360°全景視頻
/**
* 加載360度全景視頻
*/
private void load360Video() {
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);
/**設置加載設置**/
VrVideoView.Options options = new VrVideoView.Options();
options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER;
/**
* 設置加載監聽
*/
vr_video_view.setEventListener(new VrVideoEventListener() {
/**
* 視頻播放完成回調
*/
@Override
public void onCompletion() {
super.onCompletion();
/**播放完成後跳轉到開始重新播放**/
vr_video_view.seekTo(0);
setIsPlay(false);
Log.d(TAG, "onCompletion()");
}
/**
* 加載每一幀視頻的回調
*/
@Override
public void onNewFrame() {
super.onNewFrame();
seek_bar.setProgress((int) vr_video_view.getCurrentPosition());
Log.d(TAG, "onNewFrame()");
}
/**
* 點擊VR視頻回調
*/
@Override
public void onClick() {
super.onClick();
Log.d(TAG, "onClick()");
}
/**
* 加載VR視頻失敗回調
* @param errorMessage
*/
@Override
public void onLoadError(String errorMessage) {
super.onLoadError(errorMessage);
Log.d(TAG, "onLoadError()->errorMessage=" + errorMessage);
}
/**
* 加載VR視頻成功回調
*/
@Override
public void onLoadSuccess() {
super.onLoadSuccess();
/**加載成功後設置回調**/
seek_bar.setMax((int) vr_video_view.getDuration());
Log.d(TAG, "onNewFrame()");
}
/**
* 顯示模式改變回調
* 1.默認
* 2.全屏模式
* 3.VR觀看模式,即橫屏分屏模式
* @param newDisplayMode 模式
*/
@Override
public void onDisplayModeChanged(int newDisplayMode) {
super.onDisplayModeChanged(newDisplayMode);
Log.d(TAG, "onLoadError()->newDisplayMode=" + newDisplayMode);
}
});
try {
/**加載VR視頻**/
vr_video_view.loadVideoFromAsset("congo.mp4", options);
} catch (IOException e) {
e.printStackTrace();
}
/**設置聲音按鈕點擊監聽**/
volume_toggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setIsMuted(!isMuted);
}
});
/**設置播放暫停按鈕點擊監聽**/
play_toggle.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setIsPlay(!isPlay);
}
});
/**設置進度條拖動監聽**/
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
/**
* 進度條拖動改變監聽
* @param seekBar 拖動條
* @param progress 進度
* @param fromUser 是否是用戶手動操作的
*/
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
/**調節視頻進度**/
vr_video_view.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
/**
* 設置聲音開關
*
* @param isMuted 開關
*/
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.0f : 1.0f);
}
/**
* 設置播放暫停
*
* @param isPlay 播放暫停
*/
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();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155