我正在尝试使用 Naudio 通过 TCP 连接传输音频。问题是音频听起来断断续续。我相信这是因为当我尝试将样本添加到 bufferedwaveprovider 时出现异常,提示缓冲区已满。我尝试增加缓冲区大小,但结果保持不变。-----客户端代码----- 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()); } } }-----服务器代码------ 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(); } 这是我收到的确切错误“System.InvalidOperationException:NAudio.Wave.BufferedWaveProvider.AddSamples 缓冲区已满(Byte[] 缓冲区,Int32 偏移量,Int32 计数
1 回答
繁花如伊
TA贡献2012条经验 获得超12个赞
如果您遇到缓冲区已满的异常,则意味着音频到达的速度比您播放的速度快。您要么需要更大的缓冲区大小,要么在下载之前限制音频。
您还应该使用e.BytesRecorded
, 而不是e.Buffer.Length
在服务器端。这也可能解释了您所看到的问题。
另一个错误是您应该检查读取的字节数ns.Read
并在调用时使用该数字bwp.AddSamples
- 1 回答
- 0 关注
- 71 浏览
添加回答
举报
0/150
提交
取消