AlreadyAllocated调用waveOutOpen错误(AlreadyAllocated c

2019-06-25 14:36发布

 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();
        }

@马克希思: - 随着ü说我一直在使用BufferedWaveProvider尝试,但我怎么能养活bufferedWaveProvider与“audioDataStream”

Answer 1:

你不断地未设while循环,这是问题的一部分内创建waveout的对象。 在这种情况下,最好的方法是创建一个单一的waveout的对象,并使用BufferedWaveProvider喂它。 然后,音频变为可用,它写进BufferedWaveProvider。



Answer 2:

初始化新的,当我面临着类似的问题WaveOut与SineWaveProviders对象。 我碰到使用像ringlist的东西,至少在我的情况,我听不到上述5个恒正弦波任何区别交换不同供应商的值(频率和幅度)的想法。 此外,在我不同的机器上测试,使用超过6-7 WaveOut对象一次通常导致MemoryAllocationError S.这样的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在其构造。



文章来源: AlreadyAllocated calling waveOutOpen error