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
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 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.
I want to use Media Recorder to record and save an audio file to the sd card. The debugger has shown that the line recorder.start() raises an IllegalStateException. The code is taken from the android dev website and the only change is to the file name and path.
When I reach the error in the debugging menu, I am shown the View class which says:
Source not found.
The source attachment does not contain the source for the file View.class.
You can change the source attachment by clicking Change Attached Source below:
canRecord is a boolean set initially to true, which dictates which method is called in an onClick function. That functionality is working.
#SuppressLint("SimpleDateFormat")
private void record(){
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM/dd/yyyy");
String audio_path = Environment.getExternalStorageDirectory() + "/resources/resources/WI1/";
String fileName = username +"-"+ timeStampFormat.format(new Date())+".mp4";
audio_button.setText("Recording");
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(audio_path+fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Toast.makeText(this, "Can't record audio at this time", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
canRecord = false;
recorder.start();
} // record
This is the stop recording function, although it has never reached this point.
private void stopRecording(){
audio_button.setText("Attach Audio");
recorder.stop();
recorder.release();
}
Finally, I have added the following permissions to the manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How can I solve this? Thanks!
May be there is problem in filename you are giving to the recorder.Try this:
String audio_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/resources/resources/WI1/";
String fileName = audiopath+"-"+ timeStampFormat.format(new Date())+".mp4";
May it helps..
I am using libgdx(multiplatform open source game engine) and i had a similar problem when i was trying to use androids code instead of the libgdx code for recording.Turned out i needed to dispose of my libgdx recorder code or just delete it all.So perhaps you have another recorder interfering somewhere
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'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