Reputation: 192
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?
Upvotes: 1
Views: 523
Reputation: 63752
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 (e.g. 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).
Upvotes: 2