快速上手Google VR SDK进行360度全景网络直播

MrLee1月前 163

HLS (HTTP Live Streaming)是Apple的动态码率自适应技术。主要用于PC和Apple终端的音视频服务。包括一个m3u(8)的索引文件,TS媒体分片文件和key加密串文件。Google VR刚好提供了这个接口,下面来看看怎么使用吧!

核心代码

options.inputFormat = FORMAT_HLS;
vr_video_view.loadVideo(Uri.parse("http://***.***.***.**:28080/live/test.m3u8"),options);

首先创建一个空的工程,然后在gradle添加依赖库

    implementation 'com.google.vr:sdk-videowidget:1.160.0'

接上sync完成上源码

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.SeekBar;
import com.google.vr.sdk.widgets.video.VrVideoEventListener;
import com.google.vr.sdk.widgets.video.VrVideoView;
import java.io.IOException;
import static com.google.vr.sdk.widgets.video.VrVideoView.Options.FORMAT_HLS;
public class VRActivity extends Activity {
    /**
     * 播放360度全景视频的的控件
     */
    private VrVideoView vr_video_view;
    /**
     * 拖动进度的进度条
     */
    private SeekBar seek_bar;
    /**
     * 声音开关
     */
    private ImageButton volume_toggle;
    /**
     * 播放按钮
     */
    private ImageButton play_toggle;
    /**
     * 声音是否开启
     */
    private boolean isMuted;
    /**
     * 播放暂停
     */
    private boolean isPlay = true;
    /**
     * 打印调试的TAG
     */
    private final String TAG = "VrVideoView";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_vr);
        load360Video();
    }
    /**
     * 加载360度全景视频
     */
    private void load360Video() {
        vr_video_view = findViewById(R.id.vr_video_view);
        seek_bar = findViewById(R.id.seek_bar);
        volume_toggle = findViewById(R.id.volume_toggle);
        play_toggle = 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视频**/
            options.inputFormat = FORMAT_HLS;
            vr_video_view.loadVideo(Uri.parse("http://***.***.***.**:28080/live/test.m3u8"),options);
            //vr_video_view.loadVideoFromAsset("congo.mp4", options);
            vr_video_view.setDisplayMode(2);
        } 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();
        }
    }
    public boolean isMuted() {
        return isMuted;
    }
    /**
     * 在销毁时关闭视频,防止内存溢出
     */
    @Override
    protected void onDestroy() {
        vr_video_view.shutdown();
        super.onDestroy();
    }
}


本文链接:https://www.it72.com/12641.htm

推荐阅读
最新回复 (0)
    返回