Android MediaRecorder causes Force Stop error

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!

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

上一篇: 使用Android MediaRecorder

下一篇: Android MediaRecorder会导致强制停止错误