I use MediaRecorder to record audio, is it possible to detect the silence and stop recording voice when the user hasn't spoken for some seconds and record again automatically when the user recover to speak?
No, in order to detect silence you need to inspect the audio data.
MediaRecorder just writes the audio to a file as can be seen in MediaRecorder's state machine diagram.
The solution would be to use AudioRecord and analyze the data before writing the bytes to a file. Since you want to resume recording, you'll need to the keep the mic open and process the incoming audio for the user speaking.
The subject of analyzing digital audio data is known as signal processing where you can find a number of resources:
Simplest way of detecting where audio envelopes start and stop
What dBFS threshold should I set for differentiation between silence and NOT silence
But for a first attempt, I would use Audio record in android and FFT and assume a quiet room. You'll have to come up with an appropriate trigger level (see above links).
Be aware that processing does add some lag and you'll have to compensate, i.e. buffer 0 has silence, buffer 1 the user starts to speak but doesn't trigger threshold, buffer 2 user is speaking and threshold is triggered. You may want to save buffer 1 in addition to buffer 2.
2.1m questions
2.1m answers
60 comments
57.0k users