Choppy Audio with Naudio and TCP Stream. Buffer Full Exception

荒凉一梦 提交于 2020-06-29 02:56:33

问题


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

Here is the exact error I recieve

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


回答1:


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. 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



来源:https://stackoverflow.com/questions/57563843/choppy-audio-with-naudio-and-tcp-stream-buffer-full-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!