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:

  1. Recording any part of the user's surroundings, such as the audio, requires a permission:
    [assembly: UsesPermission(Manifest.Permission.RecordAudio)]
  2. To record audio, we need a MediaRecorder instance with the audio source set to the microphone:
    var recorder = new MediaRecorder();
    recorder.SetAudioSource(AudioSource.Mic);
  3. Then, we must specify in the format in which we want to persist the audio file:
    recorder.SetOutputFormat(OutputFormat.ThreeGpp);
    recorder.SetAudioEncoder(AudioEncoder.Default);
  4. Next, we specify where to save the audio file using the SetOutputFile method:
    var path = FilesDir.AbsolutePath;
    path = Path.Combine(path, "Recording", "audio.3gp");
    recorder.SetOutputFile(path);
  5. Finally, we start the recorder after preparing it:
    recorder.Prepare();
    recorder.Start();
  6. In order to stop the recorder, use the following line of code:
    recorder.Stop();
  7. If we aren't going to use the recorder again, we will free any resources that are used:
    recorder.Release();
    recorder = null;

How it works...

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.

Tip

Android version 5.0 supports recording an Ogg Vorbis audio in a WebM container using the Webm output format and the Vorbis audio encoder.

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.

Note

The location should be set after setting the output audio format.

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.

Note

The Stop() method will throw an exception if no data was recorded, allowing the empty files to be removed.

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.

Tip

We can use the MediaRecorder instance in a Service to record audio across activities.

There's more...

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.

Note

When the Info event is received, it is not guaranteed that the recording has actually finished yet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset