Home

Awesome

<p align="left"><img src="logo/horizontalversion.png" alt="Plugin.AudioRecorder" height="100px"></p>

Audio Recorder plugin for Xamarin and Windows NuGet

Features:

Setup

Platform Support

PlatformSupportedVersionNotes
Xamarin.iOSYesiOS 7+
Xamarin.AndroidYesAPI 16+Project should target Android framework 8.1+
Windows UWPYes10.0Build 15063 and up

Notes:

Required Permissions & Capabilities

The following permissions/capabilities are required to be configured on each platform:

Android

<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" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

Additionally, on OS versions Marshmallow and above, you may need to perform a runtime check to ask the user to access their microphone.

Example:

Do this in your main activity or at the point you'll be needing access to record audio:

if (ContextCompat.CheckSelfPermission (this, Manifest.Permission.RecordAudio) != Permission.Granted)
{
	ActivityCompat.RequestPermissions (this, new String [] { Manifest.Permission.RecordAudio }, 1);
}

iOS

For iOS 10 and above, you must set the NSMicrophoneUsageDescription in your Info.plist:

<key>NSMicrophoneUsageDescription</key>
<string>The [app name] wants to use your microphone to record audio.</string>

UWP

You must check the Internet and Microphone capabilities in your app's Package.appxmanifest file.

Usage

In a controller/activity/page, initialize a new AudioRecorderService.

Example:

recorder = new AudioRecorderService
{
	StopRecordingOnSilence = true, //will stop recording after 2 seconds (default)
	StopRecordingAfterTimeout = true,  //stop recording after a max timeout (defined below)
	TotalAudioTimeout = TimeSpan.FromSeconds (15) //audio will stop recording after 15 seconds
};

More settings and properties are defined below

Recording

To begin recording, use the StartRecording () and StopRecording () methods as shown:

async void RecordButton_Click (object sender, EventArgs e)
{
	await RecordAudio ();
}

async Task RecordAudio ()
{
	try
	{
		if (!recorder.IsRecording)
		{
			await recorder.StartRecording ();
		}
		else
		{
			await recorder.StopRecording ();
		}
	}
	catch (Exception ex)
	{
	...
	}
}

In lieu of calling StopRecording (), you can also make use of the StopRecordingAfterTimeout and/or StopRecordingOnSilence settings, which are explained below.

Using the Audio Data

Once recording has begun, there are two different ways to determine when recording has finished:

Task-based API

To use the Task-based API, you can grab the returned Task from the call to StartRecording (). This allows you to await the result of the Task, which will complete when recording is complete and return the path to the recorded audio file.

Example:

var recordTask = await recorder.StartRecording ();

... maybe do some other things like toggle your 'mic' button off while recording

//await the returned Task... this will complete once recording has been stopped
var audioFile = await recordTask;

if (audioFile != null) //non-null audioFile indicates audio was successfully recorded
{
	//do something with the file
}

Event-based API

The AudioInputReceived is raised when recording is complete, and the full filepath of the recorded audio file is passed along.

Example:

recorder.AudioInputReceived += Recorder_AudioInputReceived;

...

await recorder.StartRecording ();

...

private async void Recorder_AudioInputReceived(object sender, string audioFile)
{
	//do something with the file
}

NOTE: This event is raised on a background thread to allow for further file processing as needed. If the audioFile is null or empty, no audio was recorded.

--

There are also multiple ways to use the recorded (or recording) audio data:

Accessing the Recorded File

There are multiple ways to access the recorded audio file path:

These will all return null in the case that no audio has been recorded yet or no audio was recorded/detected in the last recording session.

Once you have the path to the recorded audio file, you can use standard file operations (for native/.NET Standard) and/or a cross platform file system abstraction like PCLStorage to get a stream to the file data.

Concurrent Streaming

It's also possible to get a stream to the recording audio data as it's being recorded, once StartRecording () has been called.

To access this readonly stream of audio data, you may call the GetAudioFileStream () method. This is useful in the case you want to immediately begin streaming the audio data to a server or other consumer.

NOTE: Since the WAV header is written after recording, once the audio length is known, the provided Stream data will contain the PCM audio data only and will not contain a WAV header. If your use case requires a WAV header, you can call AudioFunctions.WriteWaveHeader (Stream stream, int channelCount, int sampleRate, int bitsPerSample), which will write a WAV header to the stream with an unknown length.

Since GetAudioFileStream () will return a Stream that is also being populated concurrently, it can be useful to know when the recording is complete - the Stream will continue to grow! This can be accomplished with either the Event-based API or the Task-based API (which is often more useful).

An example of the Task-based API and concurrent writing and reading of the audio data is shown in the sample accompanying the Xamarin.Cognitive.Speech library. This speech client will stream audio data to the server until the AudioRecordTask completes, signaling that the recording is finished.

Properties and Settings

Limitations

Samples

Complete samples demonstrating audio recording (AudioRecorderService) and playback (AudioPlayer) of the recorded file are available in the /Samples folder:

Contributing

Contributions are welcome. Feel free to file issues and pull requests on the repo and they'll be reviewed as time permits.

Building

This solution requires the following environment/config:

Debugging

The easiest way to debug this library is to clone this repo and copy the shared library + platform project (of the platform you're debugging with) into your solution folder, then include the projects in your solution and reference them directly, rather than referencing the NuGet package.

About

License

Licensed under the MIT License (MIT). See LICENSE for details.