Domanda

I am trying to get the media recorder to work. The problem is that my code crashes when I prepare my Media recorder. Like so: mMediaRecorder.prepare()

What happens is, that it throws an IOException.

On this discussion https://code.google.com/p/android/issues/detail?id=5050 I read that the problem is that the there needs to be sufficient time before and after you call mMediaRecorder.prepare() (look at 6th post in that discussion).

Can anyone here help me out? How can I make sure that my Media recorder has enough time to prepare?

È stato utile?

Soluzione

Use an onPreparedListener to ensure it's ready

EXAMPLE:

MediaPlayer mp = new MediaPlayer();
mp.setOnPreparedListener(new OnPreparedListener(){

        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }

});

EDIT: I got this code from a tutorial in grokkingandroid.com.

MediaRecorder recorder = null;

private void startRecording(File file) {
   if (recorder != null) {
      recorder.release();
   }
   recorder = new MediaRecorder();
   recorder.setAudioSource(AudioSource.MIC);
   recorder.setOutputFormat(OutputFormat.THREE_GPP);
   recorder.setAudioEncoder(AudioEncoder.AMR_WB);
   recorder.setOutputFile(file.getAbsolutePath());
   try {
      recorder.prepare();
      recorder.start();
   } catch (IOException e) {
      Log.e("giftlist", "io problems while preparing [" +
            file.getAbsolutePath() + "]: " + e.getMessage());
   }
}

This is just an example but as you can see, the prepare() and start() methods are in the try-catch block, so if it DOES start while the MediaRecorder hasnt been fully prepared, the IOException will be caught. I think this is the only way to handle this situation.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top