Plugin.Maui.Audio
Plugin.Maui.Audio copied to clipboard
No sound after playing recording (MacCatalyst)
I'm using Blazor Hybrid with Razor pages and have some issues with playing back recorded audio on Mac. It looks like recording works fine, since the audio control loads the audio stream successfully and displays its length in seconds. However, when playing back the audio, no sound is being produced. What could the issue be?
AudioService.cs
public class AudioService : IAudioService
{
private IAudioManager _audioManager;
private IAudioRecorder _audioRecorder;
public bool IsRecording => _audioRecorder.IsRecording;
public AudioService(IAudioManager audioManager)
{
_audioManager = audioManager;
_audioRecorder = audioManager.CreateRecorder();
}
public async Task StartRecordingAsync()
{
await _audioRecorder.StartAsync();
}
public async Task<Stream> StopRecordingAsync()
{
var recordedAudio = await _audioRecorder.StopAsync();
// Tried code below as well, but also didn't work
// var player = _audioManager.CreatePlayer(recordedAudio.GetAudioStream());
// player.Play();
return recordedAudio.GetAudioStream();
}
}
Home.razor
@page "/"
@inject AudioService AudioService;
<h1>Hello, world!</h1>
Welcome to your new app.
<div class="flex-column mt3">
<div class="f5 mb2">
Record an audio
</div>
<div class="f5 mb2">
@if (!AudioService.IsRecording)
{
<button @onclick="StartRecording"> Start</button>
}
else
{
<button @onclick="StopRecording"> Stop</button>
}
</div>
<audio controls src="@AudioSource" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>
@code {
private Stream? recordedAudioStream;
private string? AudioSource { get; set; }
public async void StartRecording()
{
if (await Permissions.RequestAsync<Permissions.Microphone>() != PermissionStatus.Granted)
{
// Inform user to request permission
}
else
{
await AudioService.StartRecordingAsync();
}
}
public async void StopRecording()
{
recordedAudioStream = await AudioService.StopRecordingAsync();
var audioBytes = new byte[recordedAudioStream.Length];
await recordedAudioStream.ReadAsync(audioBytes, 0, (int)recordedAudioStream.Length);
var base64String = Convert.ToBase64String(audioBytes);
AudioSource = $"data:audio/mpeg;base64,{base64String}";
StateHasChanged();
}
}