Cannot play recorded video in Android

944 views Asked by At

I want to play a video using a mediaPlayer taken by my own app in Android.

I setup the mediaPlayer this way:

private void setUpMediaRecorder() throws IOException {
    final Activity activity = getActivity();
    if (null == activity) {
        return;
    }
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
        mNextVideoAbsolutePath = getVideoFilePath();
    }
    mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
    mMediaRecorder.setVideoEncodingBitRate(10000000);
    mMediaRecorder.setVideoFrameRate(30);
    mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    switch (mSensorOrientation) {
        case SENSOR_ORIENTATION_DEFAULT_DEGREES:
            mMediaRecorder.setOrientationHint(ORIENTATIONS.get(rotation));
            break;
        case SENSOR_ORIENTATION_INVERSE_DEGREES:
            mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
            break;
    }
    mMediaRecorder.prepare();
}

private void stopRecordingVideo() {
    mMediaRecorder.stop();
    mMediaRecorder.reset();
}

return Environment.getExternalStorageDirectory() + "/"
            + time + ".mp4"; // path, where the video should be stored

And when I want to play the taken video I get the following error: This video cannot be played.
Do you know why that isn't working? I've also tried the Android-camera2video example and also there the code isn't working.

1

There are 1 answers

0
Mick On

From your code it looks like your are using time + ".mp4" as the name.

Based on the error and the behaviour it is most likely that 'time' contains some characters which are either confusing the path resolution (e.g. /) or are confusing the file extensions somehow, maybe by adding and extra '.' for example.

If you want to append the date to your file name then you can use a formatter to get it into a 'safe' format for example:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");