jfversluis / Plugin.Maui.Audio

Plugin.Maui.Audio provides the ability to play audio inside a .NET MAUI application
MIT License
259 stars 45 forks source link

No sound after playing recording (MacCatalyst) #126

Open MaikelW opened 2 months ago

MaikelW commented 2 months ago

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();
    }
}
bijington commented 2 months ago

What version are you using?

I would expect the sounds to play but there are some things that you can check:

MaikelW commented 2 months ago

I'm using version 3.0.0. Double checked if my system was muted, which wasn't the case. Also implemented those playback options, but still no sound unfortunately...

bijington commented 2 months ago

I am not sure here, I can confirm that using the CreatePlayer method and playing the recording through that player works as expected.

MaikelW commented 2 months ago

Weird... Could you send me your example to see if I can get that to work? And are you using Razor pages?

bijington commented 2 months ago

Weird... Could you send me your example to see if I can get that to work? And are you using Razor pages?

It's just using the sample app in this repository. It's not using Razor pages, just plain MAUI but the playback code is agnostic to the UI framework so it should work in Razor. It just won't render the playback controls for you

MaikelW commented 2 months ago

I'll get the sample app up and running and will then come back to you. Thanks for your help so far!

MaikelW commented 2 months ago

Seems to be working just fine in the sample app indeed. I'll see if I can figure out why it's not working in my razor pages app.

bijington commented 2 months ago

Good luck and let us know how you get on

MaikelW commented 2 months ago

Alright, I think it might have something to do with the thread that is responsible for playing the audio. When I record and play back the audio multiple times in a row, sometimes it works. Might be interesting to see if someone else also experiences this behavior when using razor pages.

Khongchai commented 1 month ago

I'm using razor pages and am also facing this issue. Did you manage to get it working? @MaikelW

I checked the generated file from the audio recorder, the entire file basically just contains silence (a bunch of 0s in the hex data except the header), but with correct duration, it's as if the microphone was not used somehow but the bytes are being streamed to the file. The yellow microphone icon at the top right

image << expected to see this icon but did not

didn't even show up during recording.

And like @MaikelW , I also tried the official sample project and it works fine.

MaikelW commented 1 month ago

Nope, unfortunately I wasn't able to fix the issue in my razor pages project. But I'm indeed experiencing the exact same thing as you are @Khongchai. Sometimes the audio file is indeed filled with just 0s, but with the correct duration.

ronnycsharp commented 1 week ago

It's fixed now. I've searched hours. To solve it, put this into your entitlements.plist

    <key>com.apple.security.device.audio-input</key>
    <true/>