I have successfully utilised NateRickard's three year old Nuget Plugin.AudioRecorder to make audio recordings and play them back.
I have been careful to set both StopRecordingOnSilence and StopRecordingAfterTimeout to false to allow extended recordings that can only finish on a button click.
When recording on Android, the eventual playback will automatically invoke the speaker so I can hear out loud what I have just recorded.
When recording on iOS, I can hear what I recorded when the phone is at my ear or through headphones but I want to set the option of playing it through the speaker. How do I do that within the "PlaySelectedAudioButton_Clicked"?
Here is the C# code-behind, AudioPage.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Essentials;
using Plugin.AudioRecorder;
namespace HandsFreeNotes.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class AudioPage : ContentPage
{
private readonly AudioRecorderService audioRecorderService = new AudioRecorderService()
{
StopRecordingOnSilence = false,
StopRecordingAfterTimeout = false
};
private readonly AudioPlayer audioPlayer = new AudioPlayer();
public AudioPage()
{
// add a bit of padding to cater to the "notch" on the iPhone.
if (Device.RuntimePlatform == Device.iOS) { Padding = new Thickness(0, 40, 0, 0); }
InitializeComponent();
}
private async void BackButton_Clicked(object sender, EventArgs e)
{
await Navigation.PopModalAsync();
}
async void RecordNewAudio_Clicked(object sender, EventArgs e)
{
var status = await Permissions.RequestAsync<Permissions.Microphone>();
if (status != PermissionStatus.Granted)
return;
if (audioRecorderService.IsRecording)
{
await audioRecorderService.StopRecording();
AudioImage.Source = "flatlineblue.png";
RecordNewAudio.Text = "Record New Audio";
RecordNewAudio.TextColor = Color.Black;
try
{
// Use default vibration length
Vibration.Vibrate();
}
catch (FeatureNotSupportedException ex)
{
// Feature not supported on device
// await DisplayAlert("Alert", "Vibrate is not supported on this device", "Continue");
}
catch (Exception ex)
{
// Other error has occurred.
// await DisplayAlert("Alert", "Vibrate is not supported on this device", "Continue");
}
// audioPlayer.Play(audioRecorderService.GetAudioFilePath());
// await DisplayAlert("Alert", "Recorded audio is at: " + audioRecorderService.GetAudioFilePath(), "Continue");
await DisplayAlert("Alert", "Recorded audio is at: " + audioRecorderService.FilePath, "Continue");
}
else
{
try
{
// Use default vibration length
Vibration.Vibrate();
}
catch (FeatureNotSupportedException ex)
{
// Feature not supported on device
// await DisplayAlert("Alert", "Vibrate is not supported on this device", "Continue");
}
catch (Exception ex)
{
// Other error has occurred.
// await DisplayAlert("Alert", "Vibrate is not supported on this device", "Continue");
}
AudioImage.Source = "audiocrop.gif";
RecordNewAudio.Text = "Recording... Press to Finish";
RecordNewAudio.TextColor = Color.Red;
await audioRecorderService.StartRecording();
}
}
private void PlaySelectedAudioButton_Clicked(object sender, EventArgs e)
{
try
{
Vibration.Vibrate();
// audioPlayer.Play(audioRecorderService.GetAudioFilePath());
audioPlayer.Play(audioRecorderService.FilePath);
}
catch (Exception ex)
{
// an error has occurred.
}
}
}
}