I am tried to record audio using MediaRecorder:
MediaRecorder recorder= new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);//
recorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.setMaxDuration(30*60*1000);
recorder.prepare();
recorder.start();
Code works fine no exception occur at run time, some times file not created on SDCard if file will create then file size is 0KB.
I have also register Error and Info listeners OnInfoListener OnErrorListener in
OnInfoListener public void onInfo(MediaRecorder mr, int what, int extra) returns what=802 and extra=6
I have tried this code on real device but not works.
try this its working for me:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
Here is an excellent reference for MediaRecorder to capture media.
http://developer.android.com/guide/topics/media/audio-capture.html
The sample code used in the tutorial works without any changes.
(Just make sure you added the necessary permissions in the manifest file)
You can try this:
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("start", "prepare() failed");
}
mRecorder.start();
if you getting any error then please paste logcat.
Related
so from my understanding is that the mediarecorder saves audio samples and binds them with a timeStamp to be able to seek through the audio file later (by the help of a prgressbar):
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
recorder.start();
My question
Is there any way to get these timestamps of audio samples real time while recording a voice?
please I need your help.
I am going to create a program that recording voice call but MediaRecorder.AudioSource.VOICE_CALL does not work. I think its not support on many phones.
I'm trying with MediaRecorder.AudioSource.MIC but it just records and upload voice. In another word record my voice on microphone!!!
And I'm trying with MediaRecorder.AudioSource.VOICE_COMMUNICATION but it has a bad sound.
Here is my sample code:
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setOutputFile(mFileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e("LOG", "prepare() failed");
}
recorder.start();
Please help me. Thank you.
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 have a RuntimeException when I call the method "start()" on my MediaRecorder object. I can not paste the stack trace because I have discovered the bug on Google Analytics.
This is the code:
MediaPlayer p = new MediaPlayer();
final MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
final String path = getOutputAudioFilePath(activity);
if (path == null)
return;
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.setMaxDuration(300000);
try {
recorder.prepare();
} catch (IOException e) {
Toast.makeText(activity,e.getMessage(),Toast.LENGTH_LONG).show();
}
recorder.start();
I run your code and it's all right. It works. But I used
final String path = getFilesDir().getAbsolutePath()+"/myFile"
instead.
So make sure your getOutputAudioFilePath(activity) method is returning a valid path and a path that does not require manifest permissions you haven't added, cause it might lead to the exception you are getting.
How to put on pause? I have not found such a function. There are other ways?
You can try MediaRecorder class available in Android. It has options to start and stop your recording
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
https://developer.android.com/reference/android/media/MediaRecorder.html