KristofferStrube / Blazor.MediaStreamRecording

A Blazor wrapper for the MediaStream Recording browser API.
MIT License
20 stars 3 forks source link

Decoding audio data causes EncodingErrorException #1

Open fyang93 opened 2 weeks ago

fyang93 commented 2 weeks ago

I was trying to decode each blob into raw audio byte array, but DecodeAudioDataAsync caused Kristofferstrube.Blazor.WeblDL.Exceptions.EncodingErrorException with message "Failed to execute 'decodeAudioData' on 'BaseAudioContext': Unable to decode audio data". Here is my code, do you have any suggestions?

var recorderOptions = new MediaRecorderOptions
    {
        MimeType = "audio/webm; codecs=opus",
    };
audioRecorder = await MediaRecorder.CreateAsync(JS, audioStream, recorderOptions);

audioDataAvailableEventListener = await EventListener<BlobEvent>.CreateAsync(JS, async (BlobEvent e) =>
{
    var blob = await e.GetDataAsync();
    var audioData = await blob.ArrayBufferAsync();
    if (audioData.Length > 0)
    {
        var audioBuffer = await audioContext.DecodeAudioDataAsync(audioData);
        ...
    }
});
await audioRecorder.AddOnDataAvailableEventListenerAsync(audioDataAvailableEventListener);
KristofferStrube commented 2 weeks ago

Hey @fyang93 thanks for your interest in the library. 😁

The DecodeAudioDataAsync method throws an EncodingErrorException if MIME Sniffing §6.2 Matching an audio or video type pattern doesn't find a MIME type or if it failed to decode it into a linear PCM (pulse code modulation).

When OnDataAvailable is triggered each part isn't actually a valid audio bit alone as it is meant to be continued until the recording stops. So you need to connect all the parts of the recording before decoding for it to be successful.

Could you elaborate on what your use case is? Then I might be able to make a demo that does just that or point you towards some other documentation. :)