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?
Related
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
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;
...
I have an app that encodes in amr_nb format and output the file in amr. I want the recorded file to be broken into a series of amr files of 2 KB each. Got no clue on how to achieve this.
Here is the function called upon clicking Record Button
private void startRecording() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Here is the method called upon clicking Stop Button,
private void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}
http://developer.android.com/reference/android/media/MediaRecorder.html#setMaxFileSize(long)
Then register your listener to MediaRecorder that gets a callback when the max file size is reached, then set up MediaRecorder to record the next file. Be aware there's a (good?) chance this will not produce a series of gapless files.
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();
I've used following code as base to create a recorder. I can start and stop audio recording and it gets saved properly in the location. But now I have a requirments to pause the voice recorder
How to pause the audio recorder? And resume voice recording? I've seen a voice recording appliation in my samsung galaxy Ace, it has a pause button.
Can someone enlighten me.
public class audio {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public audio(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gpp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
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.");
}
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.reset();
recorder.release();
}
public void pause() {
}
}
check out these 2 pages
http://developer.android.com/guide/topics/media/index.html
http://developer.android.com/reference/android/media/MediaRecorder.html
according to the first article there is a pause()
but i dont see a pause() method on that second link so im not sure
the other thing that the first article references:
"When you call stop(), however, notice that you cannot call start() again until you prepare the MediaPlayer again.
Always keep the state diagram in mind when writing code that interacts with a MediaPlayer object, because calling its methods from the wrong state is a common cause of bugs."
so maybe u can just stop() prepare mediaplayer then start() again