Android - Play video in new activity

2019-09-15 16:40发布

问题:

I am trying to play a selected video in another activity by passing the fileUri to the next activity. At the moment I click on (Button) findViewById(R.id.pickVid); which calls PICK_VIDEO_REQUEST once the video is selected the new activity opens but the screen is black. I don't think the fileUri is passed correctly, any help please?

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button pickVid = (Button) findViewById(R.id.pickVid);


    //choose the video
    pickVid.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent chooseVideo = new Intent(Intent.ACTION_GET_CONTENT);
            chooseVideo.setType("video/*");
            startActivityForResult(chooseVideo, PICK_VIDEO_REQUEST);
        }
    });


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_VIDEO_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri vidFile = data.getData();
            Intent playVid = new Intent(MainActivity.this, PlayVideoAct.class);
            playVid.putExtra("vidFile", vidFile);
            startActivity(playVid);
        }
    }

}

PlayVideoAct

    String mStringFilePath;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playvideo);

    mStringFilePath = getIntent().getStringExtra("vidFile");

    }

    public void surfaceCreated(SurfaceHolder holder) {

    if (this.mPlayer == null) {
        this.mPlayer = new MediaPlayer();
    } else {
        this.mPlayer.reset();
        mPlayer.start();
    }
    try {
        FileInputStream paramSurfaceHolder = new FileInputStream(mStringFilePath);
        this.mPlayer.setDataSource(paramSurfaceHolder.getFD());
        this.mPlayer.setDisplay(this.mSurfaceHolder);
        this.mPlayer.prepare();
        this.mPlayer.start();
        this.mPlayer.pause();
        Play();

    } catch (Exception e) {
        LogUtil.e(e, "Error in PlayVideoAct.surfaceCreate(SurfaceHolder)");
    }
}


private void Play() {

    mMediaPlayer.start();
    if (this.mMediaPlayer.isPlaying()) {
        this.mMediaPlayer.pause();
        return;
    }
    if (this.isStop) {
        this.mMediaPlayer.seekTo(this.leftPosition);
    }
    this.mImageViewButtonControls.setImageResource(R.drawable.pause);

}