2

I am trying to record video from android camera2 sample

All works fine but when I play the video after recording (from the sd card), the video starts freezing and you can hear just the audio at the background and when the audio stops, the video start playing without the audio and the time of the video jumps from seconds to minites (03:24)

private void setUpMediaRecorder() throws IOException {
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mMediaRecorder.setOutputFile(getVideoFile().getAbsolutePath());
    mMediaRecorder.setVideoEncodingBitRate(10000000);
    mMediaRecorder.setVideoFrameRate(30);
    //mMediaRecorder.setMaxDuration(60000); // Set max duration 60 sec.
    mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);


    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    int orientation = ORIENTATIONS.get(rotation);

    if (mCameraType == 1) {
        if (orientation == 90) {
            mMediaRecorder.setOrientationHint(270);
        } else if (orientation == 270) {
            mMediaRecorder.setOrientationHint(90);
        }
    }
    else {
        mMediaRecorder.setOrientationHint(orientation);
    }
    mMediaRecorder.prepare();
}

Figure out that it works without those lines:

mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

How can I make it works with sound?

The stupid solution is to restart the device. Is someone know why is that happening?

Thanks for helping

Eli Elezra
  • 525
  • 2
  • 4
  • 18
  • 1
    Could you resolve the problem? I encountered the same issue on a Samsung S7 (API Level 24) but not on a Sony Xperia tablet (API Level 22). Maybe it's some sort of hardware/firmware problem? – medonja Dec 28 '16 at 12:02
  • Possible duplicate of [Camera2 video recording without preview on Android: mp4 output file not fully playable](http://stackoverflow.com/questions/37767511/camera2-video-recording-without-preview-on-android-mp4-output-file-not-fully-pl) – luca992 Apr 05 '17 at 02:30

1 Answers1

0

To start with, try using the CamcorderProfile API to set the MediaRecorder values with MediaRecorder.setProfile.

Pick the resolution you want from the list of supported profiles, and the recommended audio/video encoding settings will be set when you call setProfile after setting the audio and video sources.

You should also use the CAMCORDER audio source, not MIC, but that shouldn't cause recording to break.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47