我有一个在从 UDP 套接字接收数据的循环内播放的 waveOut。但不幸的是,它在大约 30 秒后抛出一个错误,说“MemoryAllocationError 调用 waveOutOpen”。
我使用下面的代码来实现这一点
private void startAudioTransfer(bool startSending, IPAddress receipientIP, bool startReceiving, IPAddress senderIP)
{
if (IsAudioSupported)
{
IsAudioTransferActive = true;
audioUDPClient = new UdpClient(audioPort);
if (startSending)
{
startRecording();
audioSender = new Thread(new ParameterizedThreadStart(sendAudio));
audioSender.Start(receipientIP);
}
if (startReceiving)
{
audioReceiver = new Thread(new ParameterizedThreadStart(receiveAudio));
audioReceiver.Start(senderIP);
}
}
else
{
MessageBox.Show("Audio Transfer is not supported by your computer", "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
}
private void receiveAudio(object sender)
{
IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
MemoryStream audioDataStream;
RawSourceWaveStream receivedRawAudioData;
byte[] receivedAudioData;
while (IsAudioTransferActive)
{
receivedAudioData = audioUDPClient.Receive(ref senderEP);
waveOutStream = new WaveOut();
audioDataStream = new MemoryStream(receivedAudioData);
receivedRawAudioData = new RawSourceWaveStream(audioDataStream, audioFormat);
waveOutStream.Init(receivedRawAudioData);
waveOutStream.Play();
}
waveOutStream.Stop();
waveOutStream.Dispose();
}