从Android设备获取的视频文件无法在应用中播放

时间:2017-04-12 10:55:39

标签: android video

我正在尝试在我的应用中附加视频文件。我尝试使用以下代码从Android设备获取视频。附加视频后,如果我尝试使用MediaController类播放,则屏幕变为空白。请帮帮我。

void pickVideo() {
    Intent videoIntent = new Intent(Intent.ACTION_GET_CONTENT);
    videoIntent.setType("video/*");
    startActivityForResult(videoIntent, PICK_VIDEO_FILE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        try {

            if (resultCode != Activity.RESULT_OK)
                return;

            switch (requestCode) {

                case PICK_VIDEO_FILE:
                    Uri videoUri = data.getData();

                    if (mChooseFileDialogListener != null) {
                        mChooseFileDialogListener.onVideoClick(videoUri, ViewModel.FILE_TYPE_VIDEO);
                    }
                    break;
            }
        }

        ChooseFileDialogFragment.ChooseFileDialogListener mChooseFileDialogListener = new ChooseFileDialogFragment.ChooseFileDialogListener() {

            @Override
            public void onVideoClick(Uri videoUri, int fileType) {
                mPath = videoUri.toString();
            }

        }

        AttachmentAdapter.ItemClickListener = new AttachmentAdapter.ItemClickListener() {

            @Override
            public void onClick(String path) {
                playVideo(path);
            }
        }

        private void playVideo(String path) {

            MediaController mediaControls = new MediaController(getActivity());

            try {
                //set the media controller in the VideoView
                mBinding.videoPlayer.setMediaController(mediaControls);

                //set the uri of the video to be played
                if (file != null) {
                    mBinding.videoPlayer.setVideoPath(path);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

0 个答案:

没有答案