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.