Android開發(fā)VR實(shí)戰(zhàn)之播放360度全景視頻
VR即Virtual Reality虛擬現(xiàn)實(shí)。虛擬現(xiàn)實(shí)技術(shù)是一種可以創(chuàng)建和體驗(yàn)虛擬世界的計(jì)算機(jī)仿真系統(tǒng)它利用計(jì)算機(jī)生成一種模擬環(huán)境是一種多源信息融合的交互式的三維動態(tài)視景和實(shí)體行為的系統(tǒng)仿真使用戶沉浸到該環(huán)境中。
那么,如何在Android中去開發(fā)VR功能的APP呢?我們利用谷歌提供的開源SDK去實(shí)現(xiàn)一個360°全景視頻的功能
一.在build.gradle中引入谷歌VR的SDK依賴
compile 'com.google.vr:sdk-videowidget:1.10.0'
二.注意支持的最小SDK
minSdkVersion 19
targetSdkVersion 25
三.界面布局文件
<?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開發(fā)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>
四.加載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); /**設(shè)置加載設(shè)置**/ VrVideoView.Options options = new VrVideoView.Options(); options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER; /** * 設(shè)置加載監(jiān)聽 */ vr_video_view.setEventListener(new VrVideoEventListener() { /** * 視頻播放完成回調(diào) */ @Override public void onCompletion() { super.onCompletion(); /**播放完成后跳轉(zhuǎn)到開始重新播放**/ vr_video_view.seekTo(0); setIsPlay(false); Log.d(TAG, "onCompletion()"); } /** * 加載每一幀視頻的回調(diào) */ @Override public void onNewFrame() { super.onNewFrame(); seek_bar.setProgress((int) vr_video_view.getCurrentPosition()); Log.d(TAG, "onNewFrame()"); } /** * 點(diǎn)擊VR視頻回調(diào) */ @Override public void onClick() { super.onClick(); Log.d(TAG, "onClick()"); } /** * 加載VR視頻失敗回調(diào) * @param errorMessage */ @Override public void onLoadError(String errorMessage) { super.onLoadError(errorMessage); Log.d(TAG, "onLoadError()->errorMessage=" + errorMessage); } /** * 加載VR視頻成功回調(diào) */ @Override public void onLoadSuccess() { super.onLoadSuccess(); /**加載成功后設(shè)置回調(diào)**/ seek_bar.setMax((int) vr_video_view.getDuration()); Log.d(TAG, "onNewFrame()"); } /** * 顯示模式改變回調(diào) * 1.默認(rèn) * 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(); } /**設(shè)置聲音按鈕點(diǎn)擊監(jiān)聽**/ volume_toggle.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setIsMuted(!isMuted); } }); /**設(shè)置播放暫停按鈕點(diǎn)擊監(jiān)聽**/ play_toggle.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { setIsPlay(!isPlay); } }); /**設(shè)置進(jìn)度條拖動監(jiān)聽**/ seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { /** * 進(jìn)度條拖動改變監(jiān)聽 * @param seekBar 拖動條 * @param progress 進(jìn)度 * @param fromUser 是否是用戶手動操作的 */ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { /**調(diào)節(jié)視頻進(jìn)度**/ vr_video_view.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } /** * 設(shè)置聲音開關(guān) * * @param isMuted 開關(guān) */ 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); } /** * 設(shè)置播放暫停 * * @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(); } }
五.GitHub
https://github.com/linglongxin24/VRDevelopVideo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android提高之MediaPlayer播放網(wǎng)絡(luò)視頻的實(shí)現(xiàn)方法
- Android使用VideoView播放本地視頻和網(wǎng)絡(luò)視頻的方法
- 一個html5播放視頻的video控件只支持android的默認(rèn)格式mp4和3gp
- 詳解Android App中使用VideoView來實(shí)現(xiàn)視頻播放的方法
- android webvie指定視頻播放器播放網(wǎng)站視頻
- Android提高之MediaPlayer音視頻播放
- android使用videoview播放視頻
- Android播放assets文件里視頻文件相關(guān)問題分析
- Android播放視頻的三種方式
- Android仿新浪微博/QQ空間滑動自動播放視頻功能
- Android DragVideo實(shí)現(xiàn)播放視頻時任意拖拽的方法
- Android編程實(shí)現(xiàn)播放視頻的方法示例
相關(guān)文章
實(shí)現(xiàn)Android studio設(shè)置自動導(dǎo)包及自動導(dǎo)包快捷鍵
這篇文章主要介紹了實(shí)現(xiàn)Android studio設(shè)置自動導(dǎo)包及自動導(dǎo)包快捷鍵的相關(guān)資料,需要的朋友可以參考下2017-05-05Android App中使用AudioManager類來編寫音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來編寫音樂播放器的方法,文中舉了一個簡單的例子實(shí)現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04Android中利用動態(tài)加載實(shí)現(xiàn)手機(jī)淘寶的節(jié)日特效
這篇文章主要介紹了Android中利用動態(tài)加載實(shí)現(xiàn)手機(jī)淘寶的節(jié)日特效,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12Android開發(fā)Jetpack組件Room用例講解
這篇文章主要為大家介紹了Android?Jetpack組件Room的使用案例的詳細(xì)講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Android提高之多方向抽屜實(shí)現(xiàn)方法
這篇文章主要介紹了Android的多方向抽屜實(shí)現(xiàn)方法,有一定的實(shí)用價值,需要的朋友可以參考下2014-08-08Android編程實(shí)現(xiàn)自定義手勢的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義手勢的方法,結(jié)合實(shí)例形式分析了Android自定義手勢的功能、相關(guān)函數(shù)與具體實(shí)現(xiàn)步驟,需要的朋友可以參考下2016-10-10Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動畫效果
這篇文章主要介紹了Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動畫效果,對Android程序設(shè)計(jì)人員有很好的參考借鑒價值,需要的朋友可以參考下2014-08-08Kotlin中實(shí)現(xiàn)多線程數(shù)據(jù)刷新的完整方案
這篇文章主要介紹了Kotlin中實(shí)現(xiàn)多線程數(shù)據(jù)刷新的完整方案,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析
這篇文章主要介紹了Android中新引進(jìn)的Google Authenticator驗(yàn)證系統(tǒng)工作原理淺析,需要的朋友可以參考下2014-10-10