Android - 在新活动中播放视频

时间:2017-03-27 06:30:06

标签: java android media-player

我正在尝试通过将fileUri传递给下一个活动来播放另一个活动中的选定视频。目前,我点击(Button) findViewById(R.id.pickVid);,在选择视频后调用PICK_VIDEO_REQUEST新活动即会打开,但屏幕为黑色。我认为fileUri没有正确传递,请问有什么帮助吗?

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);

}

0 个答案:

没有答案