MediaRecorder prepare failed: -2147483648 for nougat - android

I'm writing an application to record video from camera through surface.
Following code is for preparing 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();
}
Getting outfile(videoFilename) like this,
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 but a Object Size class.
When I try to initiate MediaRecorder I get java.io.IOException: prepare failed.
Strange thing is It's crashing in Nougat OS only, in other devices It's working correctly.

I guess you are running this code on emulator. Actually MediaRecorder is not supported on emulator.
see:MediaRecorder
Note: Currently, MediaRecorder does not work on the emulator.
Well, you can test that by comment Mic config and you will see its working.
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 its working. Issue on emulator only.

For me, the MediaRecorder -2147483648 error
was device and OS level dependent. Usually, the recorder
would start 1 time and subsequent starts would fail.
The fix was to add a call to MediaRecorder.release().
...
try {
audioRecorderType_MR.stop();
audioRecorderType_MR.release(); // <<-- ADD THIS LINE
} catch (Exception e) {
J42CallerId.printStackTrace(new J42ProgramCheck("J42VM0072E:", e));
}
audioRecorderType_MR = null;
...

Related

MediaRecorder does not record audio in the background

We call the startRecord method from the Firebase JobService. When our application is in the foreground, sound recording from the microphone passes without problems. However, if the application is in the background (no foreground activity), the MediaRecorder records silence. The recording also happens without problems if you call startRecord from the foreground service, however, our application must make a hidden recording. Here is the startRecord method:
void startRecord(int duration) {
audioFile = Environment.getExternalStorageDirectory() + "/record.amr";
try {
File outFile = new File(audioFile);
if (outFile.exists()) {
outFile.delete();
}
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(audioFile);
mediaRecorder.prepare();
mediaRecorder.start();
//start timer for stop recording
handler.postDelayed(StopRecordTask, duration * 1000);
Log.d(LOG_TAG, "Audio record start");
} catch (Exception e) {
if (mediaRecorder != null) {
mediaRecorder.release();
mediaRecorder = null;
}
errorMessage();
e.printStackTrace();
}
}
We use android SDK version 26. Tell me, please, what is the cause of the problem?

Call Recording Other side voice volume is very low in recorded file

This is the function called in a service for call recording.
try{
mediaRecorder = new MediaRecorder();
file = File.createTempFile("" + cal.getTime(), ".amr", fileDirPath);
this.phoneCall.setPathToRecording(file.getAbsolutePath());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioSamplingRate(8000);
mediaRecorder.setAudioEncodingBitRate(12200);
mediaRecorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
Tested with SAMSUNG, LENOVO, GIONEE Works fine.
The problem occurs in the MOTOROLA and MI devices
What code is need to be change to get proper other person sound in the recoded file

MediaRecorder, recording audio and video has very low volume

Im using MediaRecorder on an android project, to record video and audio files.
it records correctly but the sound volume on both files is very very low.
I use this to configure MediaRecorder to record audio
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + ConfigApp.RECORDINGS_FOLDER);
if (!folder.exists())
success = folder.mkdir();
if (success) {
mFileName = folder + timeStamp + ".m4a";
currentFile = mFileName;
chrono.setBase(SystemClock.elapsedRealtime());
chrono.start();
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(TAG, "prepare() failed");
}
mRecorder.start();
handler.post(updateVisualizer);
}
and this when i record video
// BEGIN_INCLUDE (configure_preview)
mCamera = CameraHelper.getDefaultCameraInstance();
// We need to make sure that our preview and recording video size are supported by the
// camera. Query camera to find all the sizes and choose the optimal size given the
// dimensions of our preview surface.
Camera.Parameters parameters = mCamera.getParameters();
List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
Camera.Size optimalSize = CameraHelper.getOptimalPreviewSize(mSupportedPreviewSizes,
mSurfaceView.getWidth(), mSurfaceView.getHeight());
// Use the same size for recording profile.
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoFrameWidth = optimalSize.width;
profile.videoFrameHeight = optimalSize.height;
// likewise for the camera object itself.
parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
mCamera.setParameters(parameters);
try {
// Requires API level 11+, For backward compatibility use {#link setPreviewDisplay}
// with {#link SurfaceView}
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
mCamera.setDisplayOrientation(90);
} catch (IOException e) {
Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
return false;
}
// END_INCLUDE (configure_preview)
// BEGIN_INCLUDE (configure_media_recorder)
mMediaRecorder = new MediaRecorder();
// 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 a CamcorderProfile (requires API Level 8 or higher)
mMediaRecorder.setProfile(profile);
// Step 4: Set output file
String path = getVideoFile(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO).getAbsolutePath();
currentFile = path;
mMediaRecorder.setOutputFile(path);
mMediaRecorder.setOrientationHint(270);
// END_INCLUDE (configure_media_recorder)
// Step 5: 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;
i tried other options on setAudioSource besides the AudioSource.CAMCORDER but i always get low volume
do i need to configure any extra parameters?
I had the same problem while using MediaRecorder and finally figured out the correct working solution.
Here are few modifications you need to do for good quality audio recordings:
mRecorder.setAudioEncodingBitRate(16*44100);
mRecorder.setAudioSamplingRate(44100);
Many solutions on stackoverflow would suggest .setAudioEncodingBiteRate(16) but 16 is too low to be considered meaningless .
Source: #Grant answer on stackoverflow very poor quality of audio recorded on my droidx using MediaRecorder, why?
Try to
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
then, Your AudioSource set to MIC.

MediaRecorder records bad audio file on Droid X2

I'm trying to record audio using MediaRecorder on the Droid X2, and I'm running into issues. The MediaRecorder seems to prepare and start recording just fine, but when I stop recording and try to listen to the file that is produced the playback immediately stops. Even if I try to open the audio file in the standard media player app, it immediately stops.
Here's the code I'm using to record:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mCurrentRecordingFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// mRecorder.setAudioEncodingBitRate(16);
// mRecorder.setAudioSamplingRate(44100);
try {
mRecorder.prepare();
mIsPrepared = true;
} catch (IOException e) {
Log.e("Test", "MediaRecorder prepare() failed");
e.printStackTrace();
}
if (mRecorder != null && mIsPrepared) {
mRecorder.start();
mIsRecording = true;
Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath);
}
I commented out the setAudioEncodingBitRate() call because it was causing the prepare() to fail, and just to be on the safe side I also commented out setAudioSamplingRate(). I have tried every combination of output format and audio encoder that is available and the result is always the same. I get no exceptions, and a file is created that is not empty, but it will not play back properly.
Not sure if it will help diagnose, but here's the code I use to stop recording:
if (mRecorder != null && mIsRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mIsPrepared = false;
mIsRecording = false;
Log.i("Test", "Stopped audio capture");
}
The recording code works fine on a Galaxy Nexus, Nexus S, EVO 4G, and Galaxy SII. Any idea why my audio file would be bad on the Droid X2?
Found the answer here: MediaPlayer cant play audio files from program data folder?
Problem wasn't the recording, it was the way I was setting the MediaPlayer's datasource. Though I have no idea why the stock media player app wouldn't play the file... maybe it's a permission thing. Here's the code I was using to play the file:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(fileName);
mPlayer.prepare();
mPlayer.start();
Here's the code that will work if the file is stored in the external files directory:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource((new FileInputStream(fileName)).getFD());
mPlayer.prepare();
mPlayer.start();

Thick slow recorded sound using android recorder

I'm developing an android application that records sound using Mediarecorder class
The following is a part from my code:
public void start() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if(!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
try {
recorder.setOutputFile(path);
} catch (IllegalStateException e) {
e.printStackTrace();
}
recorder.prepare();
recorder.start();
}
when the recorder stops , i played it using mediaplayer class but the result sound is very thick and slow .. what could be the problem?
As per the MediaRecorder documentation the sampling rate for AMRNB is 8kHZ and you are setting this to a different value. I suspect this as the issue.
Can you comment these lines and check if that works for you:
//recorder.setAudioEncodingBitRate(16);
//recorder.setAudioSamplingRate(44100);

Categories

Resources