错误: - 在Loop中播放时调用waveOutOpen的MemoryAllocationError

时间:2012-06-20 05:39:16

标签: wpf naudio

我有一个waveOut,它在一个接收数据的循环中播放 一个UDP套接字。但不幸的是,大约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();
        }

0 个答案:

没有答案