Very often we use applications that play sounds and allow us to trigger some actions like start, stop, random and some others… but what happen if we want to record the audio that we will manipulate? It’s easy! In this post I will teach how to do this with Plugin.AudioRecorder.
We will follow these steps:
- Adding the Plugin.AudioRecorder
- Previous configuration
- Recording – Stopping – Playing audio
- Some things that you must know
First of all.. What do I need?
✔ Add from NuGet Package the plugin: Plugin.AudioRecorder:
? Previous Configurations
Once you have added the plugin, you must add the permission and capabilities configurations for each platform that you will need:
Android MODIFY_AUDIO_SETTINGS WRITE_EXTERNAL_STORAGE READ_EXTERNAL_STORAGE RECORD_AUDIO Important: For Marshmallow version and above, you need to check the microphone access at runtime.
On IOS 10 and above,in your Info.plist set a string type:
NSMicrophoneUsageDescription.In this string place a brief explanation about why do you need this permission.
Windows in the file
Package.appxmanifest,add the following capabilities:
Initialize AudioRecorderService class. This class allows us to get and set different types of behaviors for our recorder:
I will explain some of the most important methods and properties for me:
▪ StopRecordingAfterTimeOut Determines if the recording will be stopped after time out. Accepts/Returns a bool Value. (Default value: True)
▪ TotalAudioTimeOut Indicates the recording time that will be taken as time out to stop the recording. Accepts/Returns a TimeSpan value.
▪ SilenceThreshold Indicates a signal threshold to start identifying silence for our recorder, if we set the StopRecordingOnSilence property, the silence detector can be triggered ahead of time. Accepts/Returns a Float Value bettween 0 and 1 and default value its 2.
▪ FilePath Determines the file path of the recorder. If null, it will set a temporary file (With GetAudioFilePath method ). Accepts/Returns a string value.
▪ IsRecording Indicates if your application is currently recording. Returns a bool value.
▪ StopRecordingOnSilence Indicates if the audio recording in the application will be able to detect a silent in the audio and stop the recording. Accepts/Returns a Bool value. (Default value: True)
▪ AudioSilenceTimeOut Indicates the recording time that will be taken as time out to stop the recording when a silent is detected. Accepts/Returns a TimeSpan Value.
Properties can be set either when we create the instance or after. Let’s see an example about how to do this:
Recording audio ?
To start recording, we just use the StartRecording method.
This plugin is a Task-based API and that’s the reason why we need to use the await in the StartRecording method.
Stopping audio ? + ?
To stop our recording, we just use the StopRecording method.
This plugin is a Task-based API and that’s the reason why we need to use the await in the StopRecording method.
Playing audio ? + ?
To reproduce our audio recorder the first thing we have to do is to initialize the AudioPlayer class:
Where you instantiated the AudioRecorderService class, add the event handler FinishedPlaying that indicates how will it behave when finishing playing the audio:
In my case I just added a change with the color of the buttons:
After, we have to get audio recorder file path, we have two ways to get it:
▪GetAudioFilePath gets file path from temporary memory.
▪GetAudioFileStream gets file path from a existing file folder.
Finally, you’ll play your audio:
Some things that you must to know
⚠ Limitations: The file can be only saved with the WAV audio format.
Supports following platforms versions:
API 16 or higher – Project should target Android framework 8.1 or higher
Version 10.0 – Build 15063 and up.
iOS 7 or higher.
Good luck with your implementation! ?
Click to see an example in my GitHub.