MediaRecorder error: -2147483648 for nougat

I am writing an application to record video from a camera over a surface.

The following code is for preparing the MediaRecorder.

MediaRecorder mediaRecorder;
mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setOutputFile(videoFilename);
        mediaRecorder.setVideoEncodingBitRate(1000000);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        try {
            mediaRecorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

      

How to get outfile (videoFilename) for example

File defaultExternalFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File videoFolder = new File(defaultExternalFolder, "My-Vids");
    if (!videoFolder.exists()) {
        videoFolder.mkdir();
    }

String timestemp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String prepend = "MyVid_" + timestemp + "_";
File videoFile = File.createTempFile(prepend, ".mp4", videoFolder);
videoFilename = videoFile.getAbsolutePath();
return videoFile;

      

videoSize is nothing more than an object size class.

When I try to start MediaRecorder I get java.io.IOException: prepare failed.

The weird thing is that it only crashes in Nougat OS, it works correctly on other devices.

+3


source to share


1 answer


I think you are using this code for an emulator. Actually MediaRecorder is not supported on the emulator. see MediaRecorder

Note. Currently MediaRecorder does not work on the emulator.

Ok, you can check it out by doing the Mic Mic setup and you will see that it works.



MediaRecorder mediaRecorder;
mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
//mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(videoFilename);
mediaRecorder.setVideoEncodingBitRate(1000000);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.setVideoSize(videoSize.getWidth(), videoSize.getHeight());
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
    mediaRecorder.prepare();
} catch (IOException e) {
    e.printStackTrace();
}

      

I have tested your code on Android 7.1 (Samsung Galaxy S8) and it works. The only problem is with the emulator.

+1


source







All Articles