I have found ways to record sound in Xamarin.Forms either doing it using Dependency Injection or a Plug in but I am not sure how I could store the obtained file, and where I should store for all platforms. Can I get some guidance please?
The Audio file is used so that the user can set their own recording as a custom alarm for an event. Ideally it would be available even after app updates, but I am just looking for the best way to do this.
Here is the code I am using to record sound, in this example using the AudioRecorder Plugin found here:
https://github.com/NateRickard/Plugin.AudioRecorder/blob/master/Samples/Forms/AudioRecord.Forms/MainPage.xaml.cs
public partial class MainPage : ContentPage
{
private AudioRecorderService recorder;
private AudioPlayer player;
public MainPage()
{
InitializeComponent();
// initialize recorder
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(5) //audio will stop recording after 5 seconds
};
//initializer player part of same plugin
player = new AudioPlayer();
}
private async void buttonRecord_Clicked(object sender, EventArgs e)
{
await RecordAudio();
}
private async Task RecordAudio()
{
try
{
if (!recorder.IsRecording)
{
var recordTask = await recorder.StartRecording();
var audioFile = await recordTask;
if (audioFile != null)
{
// store the audioFile
}
}
else
{
await recorder.StopRecording();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}