已经全部调用waveOutOpen错误

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

标签: c# .net wpf exception-handling naudio

 private void receiveAudio(object sender)
        {
            IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
            MemoryStream audioDataStream;
            BufferedWaveProvider bufferedWaveProvider; 
            //RawSourceWaveStream receivedRawAudioData;          
            byte[] receivedAudioData;
            waveOutStream = new WaveOut();

            while (IsAudioTransferActive)
            {
                receivedAudioData = audioUDPClient.Receive(ref senderEP);               
                audioDataStream = new MemoryStream(receivedAudioData);                
                //receivedRawAudioData = new RawSourceWaveStream(audioDataStream, audioFormat);
                bufferedWaveProvider = new BufferedWaveProvider(audioFormat);
//How can I feed "audioDataStream " to "bufferedWaveProvider "
                waveOutStream.Init(bufferedWaveProvider);
                waveOutStream.Play();
            } 
            waveOutStream.Stop();
            waveOutStream.Dispose();
        }

@Mark Heath: - 正如你所说我尝试过使用BufferedWaveProvider但是如何用“audioDataStream”提供bufferedWaveProvider

2 个答案:

答案 0 :(得分:3)

您不断在while循环中创建未处理的WaveOut对象,这是问题的一部分。在这种情况下,最好的方法是创建一个WaveOut对象并使用BufferedWaveProvider提供它。然后,当音频变为可用时,将其写入BufferedWaveProvider。

答案 1 :(得分:0)

使用SineWaveProviders初始化新的WaveOut对象时,我遇到了类似的问题。我想到使用类似环形列表的东西来交换不同提供商的值(频率和幅度),至少在我的情况下,我听不到5个恒定正弦波之上的任何差异。此外,在我在不同机器上的测试中,一次使用6-7个以上WaveOut个对象通常会导致MemoryAllocationError个环列表。这就是我出来的内容:

private Queue<SineWaveGenerator> generators;

// constructor
public Player()
{
    for (int i = 0; i < 5; i++)
    {
        var generator = new SineWaveGenerator();
        generator.Amplitude = 0.25f;

        generators.Enqueue(generator);
    }
}

// just a helper method
private SineWaveGenerator GetGenerator(int frequency)
{
    return generators.FirstOrDefault(x => x.Frequency == frequency);
}

private void Play(int frequency)
{
    var generator = GetGenerator(frequency);

    if (generator == null)
    {
        generator = generators.Dequeue(); // get generator from the top of the list
        generator.Frequency = GetFrequency(key); // modify the generator
        generators.Enqueue(generator); // and append it to the back
    }

    generator.Play();
}

private void Stop(int frequency)
{
    var generator = GetGenerator(frequency);

    if (generator != null)
    {
        generator.Stop();
    }
}

注意:SineWaveGenerator在其构造函数中初始化一个新的WaveOut