Я пытаюсь записать видео с помощью средства записи мультимедиа и отобразить это видео в других действиях. Он работал нормально, и видео было, но теперь возникает ошибка, что видео не может отображаться. Может кто-нибудь помочь?
Вот мой код:
captureButton.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
isActionDown = true;
try {
if (isActionDown) {
initRecorder();
if (isActionDown)
prepareRecorder();
isPrepared = true;
}
if (isPrepared && isActionDown) {
Toast.makeText(getContext(), "startRecorder", Toast.LENGTH_LONG).show();
isRecording = true;
mediaRecorder.start();
} else {
releaseMediaRecorder();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("onLongPress Error ", e.toString());
}
return true;
}
});
captureButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
isActionDown = false;
try {
if (isRecording) {
if (mediaRecorder != null) {
mediaRecorder.stop();
releaseMediaRecorder(); // release the MediaRecorder object
camera.lock();
Toast.makeText(getContext(), "MediaRecoderNull", Toast.LENGTH_LONG).show();
}
isRecording = false;
if (fileUri != null) {
playVideo(getView());
}
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
break;
}
return false;
}
});
Uri fileUri = null;
Uri ImageUri;
private void initRecorder() {
mediaRecorder = new MediaRecorder();
camera.unlock();
mediaRecorder.setCamera(camera);
mediaRecorder.setOrientationHint(90);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
}
private void prepareRecorder() {
mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
try {
mediaRecorder.prepare();
isPrepared = true;
return;
} catch (IllegalStateException e) {
releaseMediaRecorder();
e.printStackTrace();
} catch (IOException e) {
releaseMediaRecorder();
e.printStackTrace();
}
}
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
return Uri.fromFile(getOutputMediaFile(type));
}
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type){
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (! mediaStorageDir.exists()){
if (! mediaStorageDir.mkdirs()){
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else if(type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
public void playVideo(View view) {
Intent playIntent = new Intent(getActivity(), CapturedVideoActivity.class);
playIntent.putExtra("videoUri", fileUri.toString());
startActivity(playIntent);
}
поэтому на следующем экране при отображении это происходит введите описание изображения здесь
Код следующего экрана:
VideoView mVideoView = findViewById(R.id.videoCaptured);
videoUri = Uri.parse(getIntent().getExtras().getString("videoUri"));
mVideoView.setVideoURI(videoUri);
mVideoView.start();