Android MediaRecorder IllegalStateException

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

}

确保包含以下权限。

<uses-permission android:name="android.permission.RECORD_AUDIO" />

可能是另一个录音处于活动状态(可能是另一个下载的应用程序的默认录音机或录音机),请停止该录音并重新进行操作,希望它可以正常工作

链接地址: http://www.djcxy.com/p/31140.html

上一篇: Android MediaRecorder会用绿线产生损坏的视频

下一篇: Android MediaRecorder IllegalStateException