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.
Related
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 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.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
MediaRecorder: setCamera() - error camera is not aviable
I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.
I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.
My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I have posted my code for the recording feature below.
EDIT: I solved my original problem. Now when I add recorder.stop() to my code I get an illegalStateException. I have updated the code below as well(The only changes are in the startRecorder method). Any ideas ?
I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.
public class MyRecorderActivity extends Activity{
private Button audio;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionandanswer);
....
....
audio = (Button) findViewById(R.id.audio_recordactivity);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath() + "/");
}
private void startRecording(){
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
}
}
I solved my problem. I forgot to add the permissions!
I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.
I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.
My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I used the debugger to find out that it is because I have an illegalStateException on recorder.stop()
I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.
I posted this question before, and solved my original problem but ran into this one after. So it may seem as a duplicate.
public class MyRecorderActivity extends Activity{
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"
private Button audio;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionandanswer);
....
....
audio = (Button) findViewById(R.id.audio_recordactivity);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath()+ "/ " + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_3GP);
}
private void startRecording(){
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
}
}
Make sure to include following permission.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
May be another recording is active(May be a default recorder or recorderr of another downloaded application)stop that recording and do the operation again, hope it works
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);