我正在尝试使用 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 计数
繁花如伊
相关分类