There may be several reasons to record an audio in an app. We could be processing the audio for speech recognition or we could just be making a game that requires some phrase from the user.
Our app can record audio using the MediaRecorder
type, which can capture audio from a variety of sources and in various formats:
[assembly: UsesPermission(Manifest.Permission.RecordAudio)]
MediaRecorder
instance with the audio source set to the microphone:var recorder = new MediaRecorder(); recorder.SetAudioSource(AudioSource.Mic);
recorder.SetOutputFormat(OutputFormat.ThreeGpp); recorder.SetAudioEncoder(AudioEncoder.Default);
SetOutputFile
method:var path = FilesDir.AbsolutePath; path = Path.Combine(path, "Recording", "audio.3gp"); recorder.SetOutputFile(path);
recorder.Prepare(); recorder.Start();
recorder.Stop();
recorder.Release(); recorder = null;
Recording an audio is useful when creating an app that will send voice messages, or if the app has to perform speech or music analysis.
The actual process of recording an audio is very simple and only has a few steps. Before we record an audio, we will require the RecordAudio
permission. If we have the permission, setting up the recorder is done in just three steps.
The first is to specify the locations of the audio source. Using the SetAudioSource()
method, we can specify that the audio should be captured from various sources. These sources include the microphone, which can be specified using the Mic
value, the camera microphone using the Camcorder
value, or even a voice call using the VoiceCall
value.
Next, we specify the type of audio that we are going to record, and this is a combination of the format and the encoder. The format is set using the SetOutputFormat()
method and can be MPEG4 using Mpeg4
, 3GPP using ThreeGpp
, or another format from the available options. The encoder is set using the SetAudioEncoder()
method, and it can be AAC, using the Aac
value, or one of the other encoders. Both the format and encoder allow us to specify Default
, which allows the system to decide the best values. The audio source has to be set prior to setting the output audio format.
Finally, we specify the save location of the recorded file. This can be a string
or a FileDescriptor
with the desired location of the audio file. This is done by passing the desired location to the SetOutputFile()
method.
Once we have set up our recorder, we can start the recording process. The first thing we do is to prepare the recorder, which gets the recorder ready using the specified options. Once the recorder is prepared, we can start the record process using the Start()
method.
We can stop the recording at any time using the Stop()
method. Before we can reuse the recorder, we will have to reset it. If no data was actually recorded before it was stopped, the Stop()
method will throw a RuntimeException
exception. We can catch this exception and then clean up any empty files.
After stopping the recorder, if we aren't going to use it again, we should release it so that resources can be freed. If the activity goes into the background, we should stop and release the recorder in the OnPause()
or OnStop()
methods.
When recording an audio, we have great control over what and how the audio is recorded.
We can specify the number of audio channels using the SetAudioChannels()
method, the encoding bitrate using the SetAudioEncodingBitRate()
method, and the sampling rate using the SetAudioSamplingRate()
method.
We can also specify duration for which the recording has to be done using the SetMaxDuration()
method, and we can specify how large the file can be using the SetMaxFileSize()
method.
If we specify the maximums, the recorder will raise the Info
event upon reaching them. If a maximum file size was specified, the What
property will be MaxFilesizeReached
, otherwise, it will be MaxDurationReached
.