6

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();
Charles
  • 50,943
  • 13
  • 104
  • 142
John Bajer
  • 61
  • 1
  • 4

4 Answers4

6

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();
laalto
  • 150,114
  • 66
  • 286
  • 303
wolfliu
  • 61
  • 1
  • 3
  • Thanks, this works... but just so others know, ideally you will need something to notify the user the recording has stopped, because they would not know. But if you look at the file that gets made, it is just the limited about of seconds assigned. So thanks. – Azurespot Apr 26 '15 at 03:36
3

Use setMaxDuration and setOnInfoListener to get a callback for notifing the UI.

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.setOnInfoListener(new MediaRecorder.OnInfoListener() {
    @Override
    public void onInfo(MediaRecorder mr, int what, int extra) {
        if(what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){
            Toast.makeText(context, "Recording stops. Limit reached", Toast.LENGTH_LONG).show();
        }
    }
});
recorder.start();
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

You should put your recorder.stop(), etc. calls in a timer

Here is a link to instructions on how to use a timer task http://developer.android.com/resources/articles/timed-ui-updates.html

Timer timer = new Timer();
timer.schedule(new FinishRecordingTask(), 100, 200);

just add this after you have called recorder.start()

ByteMe
  • 1,436
  • 12
  • 15
0

This might be little a nasty approach. But this is what I did,

once you start your mediaPlayer with mediaplayer.start(), start a Thread parallel to it like this,

 thread passMusic=new Thread(new Runnable() {

        public void run() {
            try {

                Thread.sleep(60000);
                mediaplayer.pause();
                mediaplayer.stop();
                mediaplayer.release();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    });passMusic.start();

And now you can use handlers to update your UI or something.

Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Actually using this in combination with woliu's answer is a great approach. His answer will limit the recording, but your answer is a good solution to let a user know it's done, for instance, you can make a toast appear after the time limit. Thanks! Although must post a `Toast` through an activity (not a `Thread`), but that's a separate issue, where answers are on Stack. – Azurespot Apr 26 '15 at 03:52
  • Thread.sleep not accurate! Pls read this https://stackoverflow.com/questions/17561380/android-thread-sleep-sometimes-waits-far-too-long – Сергей Jul 30 '21 at 11:44