1. 程式人生 > >Android-VR 支援流媒體

Android-VR 支援流媒體

Google對vr支援非常速度,從0.7版本開始關注gvr-Android-sdk,這個版本還是比較初級,還只能支援圖片,本地視訊,如果你選擇一個remote視訊,就會報io異常,看了下原始碼,還沒對遠端視訊做支援。
不過2周時間就對遠端視訊支援了,可以使用http訪問視訊地址,我趕緊嘗試了下,發現還是坑,只是對mp4等一些視訊格式支援,老闆要的是流媒體啊,對m3u8不支援怎麼是好,趕緊去github提問,很快就收到回覆,專案在整合EXOplayer,做個視訊的都知道,這個專案也是Google開源的,對流媒體很好的支援,看到了希望。
又等了大概一個時間,0.8版本出來了,趕緊嘗試,果然流媒體支援了,Google就是不一樣。

接下來使用Google官方的例項sdk-simplevideowidget,去播放隨意找來的一個m3u8格式視訊

SimpleVrVideoActivity中:

  private void handleIntent(Intent intent) {
        // Determine if the Intent contains a file to load.
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Log.i(TAG, "ACTION_VIEW Intent received"
); fileUri = intent.getData(); if (fileUri == null) { Log.w(TAG, "No data uri specified. Use \"-d /path/filename\"."); } else { Log.i(TAG, "Using file " + fileUri.toString()); } //修改支援格式為 FORMAT_HLS 流媒體 videoOptions.
inputFormat = Options.FORMAT_HLS; } else { Log.i(TAG, "Intent is not ACTION_VIEW. Using the default video."); fileUri = null; } // Load the bitmap in a background thread to avoid blocking the UI thread. This operation can // take 100s of milliseconds. //把url直接轉為uri傳入Task loadTask(Uri.parse("http://cache.utovr.com/s1oc3vlhxbt9mugwjz/L2_1920_3_25.m3u8")); }

一是格式修改為hls;

 videoOptions.inputFormat = Options.FORMAT_HLS;

二是把本地連線修改為自己的遠端url

  loadTask(Uri.parse("http://cache.utovr.com/s1oc3vlhxbt9mugwjz/L2_1920_3_25.m3u8"));
 class VideoLoaderTask extends AsyncTask<Pair<Uri, Options>, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Pair<Uri, Options>... fileInformation) {
            try {
                if (fileInformation == null || fileInformation.length < 1
                        || fileInformation[0] == null || fileInformation[0].first == null) {
                    videoWidgetView.loadVideoFromAsset("congo.mp4");
                } else {
                //remote視訊還要設定下fileInformation,這個沒啥好說的,根據API使用
                    videoWidgetView.loadVideo(fileInformation[0].first, fileInformation[0].second);
                }
            } catch (IOException e) {
                // An error here is normally due to being unable to locate the file.
                loadVideoStatus = LOAD_VIDEO_STATUS_ERROR;
                // Since this is a background thread, we need to switch to the main thread to show a toast.
                videoWidgetView.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(SimpleVrVideoActivity.this, "Error opening file. ", Toast.LENGTH_LONG).show();
                    }
                });
                Log.e(TAG, "Could not open video: " + e);
            }

            return true;
        }
    }

就改了這幾個地方,線上視訊播放起來了,vr開發是不是很簡單,要搞清楚原理,後續分析。