【Android開發VR實戰】二.播放360°全景視頻

來源:互聯網
上載者:User

標籤:try   ++   sage   append   ret   ddc   rip   use   volume   

轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53924006
本文出自【DylanAndroid的部落格】

【Android開發VR實戰】二.播放360°全景視頻

VR即Virtual Reality虛擬現實。虛擬現實技術是一種能夠建立和體驗虛擬世界的電腦模擬系統它利用電腦產生一種類比環境是一種多源資訊融合的互動三維動態視景和實體行為的系統模擬使使用者沉浸到該環境中。


那麼,怎樣在Android中去開發VR功能的APP呢?我們利用Google提供的開源SDK去實現一個360°全景視頻的功能


一.在build.gradle中引入GoogleVR的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開發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);        /**設定載入設定**/        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();        }        /**設定聲音button點擊監聽**/        volume_toggle.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                setIsMuted(!isMuted);            }        });        /**設定播放暫停button點擊監聽**/        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(); } }

五.GitHub

【Android開發VR實戰】二.播放360&#176;全景視頻

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.