NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

After I added an Audio Receiver on my server which receives audio chunks I get this error.

Open JanssonTheBest opened this issue 1 year ago • 3 comments

Relevant part:

private async void ReceiveAudioChunk(string data) { byte[] audioBytes = Convert.FromBase64String(data);

        if (audioBytes.Length == 0)
        {
            return;
        }

        using (MemoryStream stream = new MemoryStream(audioBytes))
        {

            WaveStream waveStream = new RawSourceWaveStream(stream, new WaveFormat(44100, 1));

            using (WaveOutEvent waveOut = new WaveOutEvent())
            {
                waveOut.DeviceNumber = 0;
                waveOut.Init(waveStream);
                waveOut.Play();

                // Wait for the playback to complete
                while (waveOut.PlaybackState == PlaybackState.Playing)
                {
                    await Task.Delay(1);
                }
                waveOut.Stop();
                waveOut.Dispose();
            }
        }
    }
    
    Exception thrown: 'NAudio.MmException' in NAudio.WinMM.dll

The program '[81324] Program.exe' has exited with code 3221225477 (0xc0000005) 'Access violation'.

JanssonTheBest avatar Jun 11 '23 16:06 JanssonTheBest

Could it be because of some memory issue maybe

JanssonTheBest avatar Jun 11 '23 16:06 JanssonTheBest

If you want to play chunks as they arrive, I recommend not opening a new WaveOutEvent device every time. Instead, use a BufferedWaveProvider that is constantly playing and place the received audio into that as it arrives

markheath avatar Jun 12 '23 09:06 markheath

f you want to play chunks as they arrive, I recommend not opening a new WaveOutEvent device every time. Instead, use a BufferedWaveProvider that is constantly playing and place the received audio into that as it arrives

Thanks, it fixed the access error, but how? Also really smooth audio now :)))

JanssonTheBest avatar Jun 12 '23 15:06 JanssonTheBest