Recording video using MediaRecorder and FileOutputStream produces video file that can't be played
Asked Answered
A

0

7

I am trying to implement the function where i can start and stop the video recording multiple times, and accumulate video data into a File.

This is how i prepare my media recorder:

private boolean prepareVideoRecorder(){
    mMediaRecorder = new MediaRecorder();

    //0 for landscape
    //90 for portrait

    //Check for available profile
    CamcorderProfile profile = null;
    if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_480P)){
        Log.d(TAG, "480p");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
    }else if(CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_720P)){
        Log.d(TAG, "720p");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_720P);
    }else{
        Log.d(TAG, "LOW");
        profile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
    }

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set profile
    mMediaRecorder.setProfile(profile);

    // Step 4: Set output file and pass media recorder the file descriptor
    if(mStoreFile == null){
        mStoreFile = MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO);
        try {
            mStoreFile.createNewFile();
        } catch (Exception e) {  
            e.printStackTrace();
        }
    }

    try {
        mOutputStream = new FileOutputStream(mStoreFile, true);
        mMediaRecorder.setOutputFile(mOutputStream.getFD());
    } catch (Exception e1) {
        e1.printStackTrace();
    }

    mMediaRecorder.setMaxDuration(30000);


    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(mPreviewSurface.getHolder().getSurface());

    //Check orientation and set hint
    switch(mOrientation){
    case ORIENTATION_PORTRAIT_NORMAL:
        mMediaRecorder.setOrientationHint(90); 
        break;
    case ORIENTATION_PORTRAIT_INVERTED:
        mMediaRecorder.setOrientationHint(270); 
        break;
    case ORIENTATION_LANDSCAPE_NORMAL:
        mMediaRecorder.setOrientationHint(0); 
        break;
    case ORIENTATION_LANDSCAPE_INVERTED:
        mMediaRecorder.setOrientationHint(180); 
        break;
    }

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }
    return true;
}

This is the button on click code:

@Override
        public void onClick(View v) {
            if (isRecording) {
                try{
                    mOutputStream.close();
                }catch(Exception ee){
                }

                // stop recording and release camera
                mMediaRecorder.stop();  // stop the recording

                mMediaRecorder.reset();
                mCamera.lock();         // take camera access back from MediaRecorder

                // inform the user that recording has stopped
                mRecordButton.setImageResource(R.drawable.record_button);
                isRecording = false;

            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                    mMediaRecorder.start();

                    // inform the user that recording has started
                    mRecordButton.setImageResource(R.drawable.record_button_on);
                    isRecording = true;
                } else {
                    // prepare didn't work, release the camera
                    releaseMediaRecorder();
                    // inform user
                }
            }
        }
    });

MediaUtil.getOutputMediaFile(MediaUtil.MEDIA_TYPE_VIDEO) will give me something like: /storage/sdcard0/Pictures/Project/VID_2013.mp4

Current problem:

The way i test it at the moment is by:

  1. Start recording
  2. Stop Recording
  3. Start recording
  4. stop recording
  5. Go to android's file manager on /Pictures/Project path
  6. I can see the file that was created and "appended" with multiple segments of data. But it won't play. And it doesn't have a cover image like other video files.

Somewhere along the line, the file is corrupted? It doesn't work as well if i just record once and check the file in the storage. I am able to record video if i just define a File for the MediaRecorder in setOutputFile, but i'm trying append video data for multiple shoot. Is this possible?

Axletree answered 29/1, 2013 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.