简体   繁体   English

带有Naudio和TCP流的不稳定音频。 缓冲区完全异常

[英]Choppy Audio with Naudio and TCP Stream. Buffer Full Exception

I am attempting to stream audio using Naudio over a TCP connection. 我正在尝试通过TCP连接使用Naudio流音频。 The problem is the audio sounds choppy. 问题是音频听起来不连贯。 I believe this is because I am getting an exception saying the buffer is full when I try to add samples to the bufferedwaveprovider. 我相信这是因为当我尝试将样本添加到bufferedwaveprovider时,我收到一个例外,说缓冲区已满。

I have tried increasing the buffer size however the result remains unchanged. 我尝试增加缓冲区大小,但是结果保持不变。

-----CLIENT CODE----- -----客户代码-----

    public TcpClient client;
    public WaveOut waveplayer = new WaveOut();
    public BufferedWaveProvider bwp = new BufferedWaveProvider(new WaveFormat(8000, 16, 1));
    public byte[] buffer = new byte[1024 * 16];

    public Form1()
    {
        bwp.BufferLength = 1024 * 16;
        waveplayer.Init(bwp);
        waveplayer.Play();
    }

    public void audio()
    {
        try
        {
            client = new TcpClient(textBox1.Text.ToString(), 8001);
            NetworkStream ns = client.GetStream();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }

        while (true)
        {
            try
            {
                ns.Read(buffer, 0, buffer.Length);
                bwp.AddSamples(buffer, 0, buffer.Length);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

-----SERVER CODE------ -----服务器代码------

    public NAudio.Wave.WaveInEvent sourcestream = null;
    public TcpListener listener = new TcpListener(IPAddress.Any, 8001);
    public TcpClient client;
    public NetworkStream ns;

    public Form1()
    {
        InitializeComponent();
        sourcestream = new NAudio.Wave.WaveInEvent();
        sourcestream.DeviceNumber = 0;            
        sourcestream.WaveFormat = new NAudio.Wave.WaveFormat(8000, 16, 1);
        sourcestream.DataAvailable += new EventHandler<NAudio.Wave.WaveInEventArgs>(audioDataAvailable);
        sourcestream.StartRecording();
    }

    public void acceptclients()
    {
        listener = new TcpListener(IPAddress.Any, 8001);
        listener.Start();
        client = listener.AcceptTcpClient();
        ns = client.GetStream();
    }

    void audioDataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
    {
        try
        {
            if (client.Connected)
            {
                ns.Write(e.Buffer, 0, e.Buffer.Length);
                ns.Flush();
            }
        }
        catch(Exception ex)
        {            

Here is the exact error I recieve 这是我收到的确切错误

"System.InvalidOperationException: Buffer full at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count “ System.InvalidOperationException:NAudio.Wave.BufferedWaveProvider.AddSamples(Byte []缓冲区的缓冲区已满,Int32偏移量,Int32计数

If you're getting a buffer full exception, that means audio is arriving faster than you are playing it. 如果您收到缓冲区已满的异常,则意味着音频到达的速度比您播放的速度快。 You either need a larger buffer size, or to throttle audio before downloading it. 您需要更大的缓冲区大小,或者在下载音频之前先对其进行节流。

You also should use e.BytesRecorded , not e.Buffer.Length on the server side. 您还应该在服务器端使用e.BytesRecorded ,而不是e.Buffer.Length That might also account for the issue you are seeing. 这也可能是您遇到的问题的原因。

Another bug is that you should examine the number of bytes read from ns.Read and use that number when calling bwp.AddSamples 另一个错误是您应该检查从ns.Read读取的字节数。调用bwp.AddSamples时请读取并使用该数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM