1

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();
   }

}
4

2 回答 2

0

确保包括以下许可。

<uses-permission android:name="android.permission.RECORD_AUDIO" />
于 2012-06-07T15:23:18.833 回答
0

可能是另一个录音处于活动状态(可能是默认录音机或另一个下载应用程序的录音机)停止该录音并再次执行操作,希望它有效

于 2013-05-24T14:32:00.600 回答