Skip to content

KSYVrPlayerBasicExample

xinbaicheng edited this page Aug 9, 2016 · 3 revisions

金山云Android VR播放SDK基本使用示例

阅读对象

本SDK面向所有使用该SDK的开发、测试人员,要求读者有一定的Android开发经验

1.基本定义

金山云Android VR播放SDK提供一个VR播放的库MDVRLibrary,这个库提供了所有关于的VR的功能

public class TestVideoActivity extends Activity {
    private static final SparseArray<String> sDisplayMode = new SparseArray<>();
    private static final SparseArray<String> sInteractiveMode = new SparseArray<>();
    private static final SparseArray<String> sProjectionMode = new SparseArray<>();
    private MDVRLibrary mVRLibrary;
    protected KSYMediaPlayer mPlayer;

    private IMediaPlayer.OnPreparedListener mPreparedListener = new IMediaPlayer.OnPreparedListener(){
        @Override
        public void onPrepared(IMediaPlayer mp) {
            cancelBusy();
            mStatus = STATUS_PREPARED;
            mPlayer.start();

        }
    };
    private IMediaPlayer.OnInfoListener mInfoListener = new IMediaPlayer.OnInfoListener(){
        @Override
        public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
            return false;
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mVRLibrary.onResume(this);
        mPlayer.start();

    }

    @Override
    protected void onPause() {
        super.onPause();
        mVRLibrary.onPause(this);

        mPlayer.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVRLibrary.onDestroy();
    }

    SpinnerHelper.with(this)
                .setData(sDisplayMode)
                .setDefault(mVRLibrary.getDisplayMode())
                .setClickHandler(new SpinnerHelper.ClickHandler() {
                    @Override
                    public void onSpinnerClicked(int index, int key, String value) {
                        mVRLibrary.switchDisplayMode(TestVideoActivity.this, key);
                    }
                })
                .init(R.id.spinner_display);

    SpinnerHelper.with(this)
                .setData(sInteractiveMode)
                .setDefault(mVRLibrary.getInteractiveMode())
                .setClickHandler(new SpinnerHelper.ClickHandler() {
                    @Override
                    public void onSpinnerClicked(int index, int key, String value) {
                        mVRLibrary.switchInteractiveMode(TestVideoActivity.this, key);
                    }
                })
                .init(R.id.spinner_interactive);

    SpinnerHelper.with(this)
                .setData(sProjectionMode)
                .setDefault(mVRLibrary.getProjectionMode())
                .setClickHandler(new SpinnerHelper.ClickHandler() {
                    @Override
                    public void onSpinnerClicked(int index, int key, String value) {
                        mVRLibrary.switchProjectionMode(TestVideoActivity.this, key);
                    }
                })
                .init(R.id.spinner_projection);
}

2.创建VR播放库对象

protected MDVRLibrary createVRLibrary() {
    return MDVRLibrary.with(this)
            // 单目模式
           .displayMode(MDVRLibrary.DISPLAY_MODE_NORMAL)
            // 交互方式
           .interactiveMode(MDVRLibrary.INTERACTIVE_MODE_MOTION)
           .asVideo(new MDVRLibrary.IOnSurfaceReadyCallback() {
               @Override
               public void onSurfaceReady(Surface surface) {
                   mPlayer.setSurface(surface);
               }
           })
           .ifNotSupport(new MDVRLibrary.INotSupportCallback() {
               @Override
               public void onNotSupport(int mode) {
                   String tip = mode == MDVRLibrary.INTERACTIVE_MODE_MOTION
                             ? "onNotSupport:MOTION" : "onNotSupport:" + String.valueOf(mode);
                   Toast.makeText(TestVideoActivity.this, tip, Toast.LENGTH_SHORT).show();
               }
            })
            .directorFactory(new DirectorFactory())
            .motionDelay(SensorManager.SENSOR_DELAY_GAME)
            .sensorCallback(new SensorEventListener() {
                @Override
                public void onSensorChanged(SensorEvent sensorEvent) {

                }

                @Override
                public void onAccuracyChanged(Sensor sensor, int i) {

                }
            })
            .pinchEnabled(true)
            .gesture(new MDVRLibrary.IGestureListener() {
                @Override
                public void onClick(MotionEvent e) {
                    Toast.makeText(TestVideoActivity.this, "onClick!", Toast.LENGTH_SHORT).show();
                }
             })
             // 非常重要,VR播放所需的GLSurfaceView
             .build(R.id.gl_view);
}

3.创建播放器

private void initMedia() {

    String path = intent.getStringExtra("path");
    mPlayer = new KSYMediaPlayer.Builder(getApplicationContext()).build();
    mPlayer.setOnPreparedListener(mPreparedListener);
    mPlayer.setOnInfoListener(mInfoListener);
    mPlayer.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
        @Override
        public boolean onError(IMediaPlayer mp, int what, int extra) {
            try{
                String error = String.format("Play Error what=%d extra=%d",what,extra);
                Toast.makeText(TestVideoActivity.this, error, Toast.LENGTH_SHORT).show();
            }catch(Exception e){
                e.printStackTrace();
            }

            return true;
        }
    });

    mPlayer.setTimeout(10, 30);
    // Maybe we can use hardware decode.
    mPlayer.setCodecFlag(KSYMediaPlayer.KSY_USE_MEDIACODEC_ALL);
    mPlayer.setBufferTimeMax(5);
    try {
        mPlayer.setDataSource(path);
        mPlayer.prepareAsync();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.开始播放

mPreparedListener中收到onPrepared回调后,调用mPlayer.start() 即可开始播放视频