简体   繁体   中英

“Buffer full” exception when BufferedWaveProvider is being filled in another thread

I have two BufferedWaveProviders and MixingSampleProvider. The first BufferedSampleProvider ( myBuffer ) is for my audio comming from soundcard. The second BufferedWaveProvider ( incomingBuffer ) is for audio comming from network (TCP).

incomingBuffer is being filled asynchronously like that:

    private static void Receive()
    {
        receiver.BeginReceive(incomingBytes, 0, incomingBytes.Length, SocketFlags.None, ReceiveCallback, null);
    }

    private static void ReceiveCallback(IAsyncResult ar)
    {
        receiver.EndReceive(ar);
        incomingBuffer.AddSamples(incomingBytes, 0 , incomingBytes.Length);
        Receive();
    }

But after 7-10 second after starting the program it finishes because of exception.
How should I fill the buffer asynchronously?

Stop ignoring the return value of EndReceive .

It tells you how many bytes of data you received. That's how many samples you have to add to your buffer (though make sure that you're not converting samples to bytes and vice versa incorrectly).

Instead, now you're simply passing your incomingBytes to the incomingBuffer whole, always - even if you only received a single byte over TCP. This is garbage data, and it fills your buffer quicker than it can drain.

In general, return values of methods and functions are meaningful, and ignoring them should only be done when you understand what you're ignoring (eg if you don't care about handling anything but the callback, ignoring the return value of BeginReceive is fine; if you need to know whether there's a pending asynchronous I/O request, you need the return value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM