4
 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 但我如何才能为 bufferedWaveProvider 提供“audioDataStream”

4

2 回答 2

3

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

于 2012-06-21T09:19:33.013 回答
0

WaveOut使用 SineWaveProviders 初始化新对象时,我遇到了类似的问题。我想到了使用类似于 ringlist 的东西来交换不同提供者的值(频率和幅度)的想法,因为至少在我的情况下,我听不到 5 个恒定正弦波以上的任何差异。此外,在我在不同机器上的测试中,一次使用超过 6-7 个WaveOut对象通常会导致MemoryAllocationErrors ,因此是 ringlist。这就是我得出的结论:

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

于 2016-01-29T20:48:16.827 回答