Recording an audio
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.
How to do it...
Our app can record audio using the MediaRecorder
type, which can capture audio from a variety of sources and in various formats:
Recording any part of the user's surroundings, such as the audio, requires a permission:
[assembly: UsesPermission(Manifest.Permission.RecordAudio)]
To record audio, we need a
MediaRecorder
instance with the audio source set to the microphone:var recorder = new MediaRecorder(); recorder.SetAudioSource(AudioSource.Mic);
Then, we must specify in the format in which we want to persist the audio file:
recorder.SetOutputFormat(OutputFormat.ThreeGpp); recorder.SetAudioEncoder(AudioEncoder.Default);
Next, we specify where to save the audio file using the
SetOutputFile
method:var path = FilesDir.AbsolutePath; path = Path.Combine(path, "Recording", "audio.3gp...