Reputation: 61
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();
Upvotes: 6
Views: 6095
Reputation: 32221
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();
Upvotes: 3
Reputation: 61
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();
Upvotes: 6
Reputation: 54322
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.
Upvotes: 0
Reputation: 1476
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()
Upvotes: 0