Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically limit Android audio recording length?

I've got this working code. I need it to record only for a limited time without any click from the user. How do I do this?

    MediaRecorder recorder = new MediaRecorder();
File outputFile = new File(file);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile.getAbsolutePath());
try {
    recorder.prepare();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
recorder.start();
recorder.setMaxDuration(60000);
// stop
recorder.stop();
recorder.reset(); 
recorder.release();
like image 459
John Bajer Avatar asked Feb 17 '12 00:02

John Bajer


1 Answers

this will done well:(use the setMaxDuration before prepare/start function )

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputFile.getAbsolutePath());

recorder.setMaxDuration(60000);
recorder.prepare();
recorder.start();
like image 89
wolfliu Avatar answered Sep 30 '22 00:09

wolfliu