VOIP服务器和客户端出错的尝试
您好,我正在创建一个 VOIP 客户端和服务器,它比我预期的要好,因为我以为我第一次尝试不会听到任何声音。但实际上,我听到自己的声音大约有一两秒钟,直到我的程序在我创建的类中抛出异常,该类是为了简化发送音频字节并实时播放它们的套接字过程而创建的。这是下面的代码:
// public function member of class DataArgs
public void PlayRealTimeAudio()
{
WaveOut o = new WaveOut();
o.DeviceNumber = 0;
o.DesiredLatency = 500;
Stream s = new MemoryStream(this.Buffer);
Raw.RawSourceWaveStream raw = new Raw.RawSourceWaveStream(s, new WaveFormat(16000, 2));
o.Init(raw); // Exception throws here after hearing my voice for a brief moment
o.Play();
o.Stop();
o.Dispose();
return;
}
// Within the Form1 class
void listener_Streaming(System.Net.Sockets.TcpClient sender, DataArgs e)
{
byte[] buf = new byte[e.Length];
buf = e.Buffer;
e.PlayRealTimeAudio();
}
PS如果您需要我显示更多代码,请询问,我很乐意向您展示。
Hi I am creating a VOIP client and server, and it is going better than I expected since I was thinking that I was not going to hear nothing for my first attempt. But actually I am hearing myself fine for about a second or two until my program throws an exception within a class I created to simplify the socket process of sending audio bytes and playing them in real time. Here is the code below:
// public function member of class DataArgs
public void PlayRealTimeAudio()
{
WaveOut o = new WaveOut();
o.DeviceNumber = 0;
o.DesiredLatency = 500;
Stream s = new MemoryStream(this.Buffer);
Raw.RawSourceWaveStream raw = new Raw.RawSourceWaveStream(s, new WaveFormat(16000, 2));
o.Init(raw); // Exception throws here after hearing my voice for a brief moment
o.Play();
o.Stop();
o.Dispose();
return;
}
// Within the Form1 class
void listener_Streaming(System.Net.Sockets.TcpClient sender, DataArgs e)
{
byte[] buf = new byte[e.Length];
buf = e.Buffer;
e.PlayRealTimeAudio();
}
P.S If you need me to show more code please ask, I will be glad to show you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常的方法是让单个 WaveOut 设备不断播放并从 BufferedWaveProvider(在最新的 NAudio 代码中可用)获取其数据,而不是不断地打开和关闭 WaveOut 设备。然后,在listener_streaming中,您只需将接收到的字节添加到BufferedWaveProvider
rather than constantly opening and closing the WaveOut device, the normal way to do this would be to have a single WaveOut device constantly playing and getting its data from a BufferedWaveProvider (available in the latest NAudio code). Then, in
listener_streaming
, you would simply add the bytes received to the BufferedWaveProvider