带有 Naudio 和 TCP 流的断断续续的音频.缓冲区满异常 [英] Choppy Audio with Naudio and TCP Stream. Buffer Full Exception

查看:220
本文介绍了带有 Naudio 和 TCP 流的断断续续的音频.缓冲区满异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 TCP 连接使用 Naudio 流式传输音频.问题是音频听起来断断续续.我相信这是因为当我尝试将样本添加到 bufferedwaveprovider 时,我收到一个异常,说缓冲区已满.

I am attempting to stream audio using Naudio over a TCP connection. 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.

我尝试增加缓冲区大小,但结果保持不变.

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)
        {            

这是我收到的确切错误

"System.InvalidOperationException: NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count) 处的缓冲区已满

"System.InvalidOperationException: Buffer full at NAudio.Wave.BufferedWaveProvider.AddSamples(Byte[] buffer, Int32 offset, Int32 count

推荐答案

如果您收到缓冲区已满异常,则意味着音频到达的速度比您播放的速度快.您要么需要更大的缓冲区大小,要么在下载之前限制音频.

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.

您还应该在服务器端使用 e.BytesRecorded,而不是 e.Buffer.Length.这也可能是您看到的问题的原因.

You also should use e.BytesRecorded, not e.Buffer.Length on the server side. That might also account for the issue you are seeing.

另一个错误是您应该检查从 ns.Read 读取的字节数,并在调用 bwp.AddSamples

Another bug is that you should examine the number of bytes read from ns.Read and use that number when calling bwp.AddSamples

这篇关于带有 Naudio 和 TCP 流的断断续续的音频.缓冲区满异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆