The AudioRecorder
class provides you with the ability to record audio from a microphone in your .NET MAUI application to a file on disk. In order to create an AudioRecorder
instance you can make use of the CreateRecorder
method on the AudioManager
class.
Note
If you want to record in the background on iOS, you will need to add a key to the Info.plist
file like show below.
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
public class AudioRecorderViewModel
{
readonly IAudioManager audioManager;
readonly IAudioRecorder audioRecorder;
public AudioPlayerViewModel(IAudioManager audioManager)
{
this.audioManager = audioManager;
this.audioRecorder = audioManager.CreateRecorder();
}
public async Task StartRecording()
{
await this.audioRecorder.StartAsync();
}
public async Task StopRecording()
{
IAudioSource audioSource = await this.audioRecorder.StopAsync();
// You can use the audioSource to play the file or save it somewhere in your application.
}
}
Note
You as the developer are responsible for cleaning up the audio files. For instance, when using StartAsync()
the random file that is generated is not cleaned up automatically.
Retrieve the file path which is in the resulting object from StopAsync()
and use that to remove the file when done. Make sure to cast the resulting IAudioSource
to the concrete type of FileAudioSource
to be able to retrieve the file path.
When calling CreateRecorder
it is possible to provide an optional parameter of type AudioRecorderOptions
, this parameter makes it possible to customize the recording settings at the platform level. Note that currently you can only customize options for iOS and macOS.
The following example shows how to enable both recording (input) and playback (output) of audio:
audioManager.CreateRecorder(
new AudioRecorderOptions
{
#if IOS || MACCATALYST
Category = AVFoundation.AVAudioSessionCategory.PlayAndRecord
#endif
});
Once you have created an AudioRecorder
you can interact with it in the following ways:
The AudioRecorder
class provides the following properties:
Gets whether the device is capable of recording audio.
Gets whether the recorder is currently recording audio.
The AudioRecorder
class provides the following methods:
Start recording audio to disk in a randomly generated file.
Start recording audio to disk in the supplied filePath.
Stop recording and return the IAudioSource
instance with the recording data.
In order to record audio some platforms require some extra additional changes.
The AndroidManifest.xml file will need to be modified to include the following uses-permission
inside the manifest
tag.
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For a full example of this change check out our AndroidManifest.xml file.
The Info.plist file will need to be modified to include the following 2 entries inside the dict
tag.
<key>NSMicrophoneUsageDescription</key>
<string>The [app name] wants to use your microphone to record audio.</string>
Replacing [app name] with your application name.
For a full example of this change check out our Info.plist file.
This change is identical to the iOS section but for explicitness:
The Info.plist file will need to be modified to include the following 2 entries inside the dict
tag.
<key>NSMicrophoneUsageDescription</key>
<string>The [app name] wants to use your microphone to record audio.</string>
Replacing [app name] with your application name.
For a full example of this change check out our Info.plist file.
The Package.appxmanifest file will need to be modified to include the following entry inside the Capabilities
tag.
<DeviceCapability Name="microphone"/>
For a full example of this change check out our Package.appxmanifest file.
For a concrete example of recording audio in a .NET MAUI application check out our sample application and specifically the AudioRecorderPageViewModel
class.