C#基于UDP实现的P2P语音聊天工具(1)
这篇文章主要是一个应用,使用udp传送语音和文本等信息。在这个系统中没有服务端和客户端,相互通讯都是直接相互联系的。能够很好的实现效果。
语音获取
要想发送语音信息,首先得获取语音,这里有几种方法,一种是使用DirectX的DirectXsound来录音,我为了简便使用一个开源的插件NAudio来实现语音录取。 在项目中引用NAudio.dll
- //------------------录音相关-----------------------------
- private IWaveIn waveIn;
- private WaveFileWriter writer;
- private void LoadWasapiDevicesCombo()
- {
- var deviceEnum = new MMDeviceEnumerator();
- var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToList();
- comboBox1.DataSource = devices;
- comboBox1.DisplayMember = "FriendlyName";
- }
- private void CreateWaveInDevice()
- {
- waveIn = new WaveIn();
- waveIn.WaveFormat = new WaveFormat(8000, 1);
- waveIn.DataAvailable += OnDataAvailable;
- waveIn.RecordingStopped += OnRecordingStopped;
- }
- void OnDataAvailable(object sender, WaveInEventArgs e)
- {
- if (this.InvokeRequired)
- {
- this.BeginInvoke(new EventHandler<WaveInEventArgs>(OnDataAvailable), sender, e);
- }
- else
- {
- writer.Write(e.Buffer, 0, e.BytesRecorded);
- int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);
- if (secondsRecorded >= 10)//最大10s
- {
- StopRecord();
- }
- else
- {
- l_sound.Text = secondsRecorded + " s";
- }
- }
- }
- void OnRecordingStopped(object sender, StoppedEventArgs e)
- {
- if (InvokeRequired)
- {
- BeginInvoke(new EventHandler<StoppedEventArgs>(OnRecordingStopped), sender, e);
- }
- else
- {
- FinalizeWaveFile();
- }
- }
- void StopRecord()
- {
- AllChangeBtn(btn_luyin, true);
- AllChangeBtn(btn_stop, false);
- AllChangeBtn(btn_sendsound, true);
- AllChangeBtn(btn_play, true);
- //btn_luyin.Enabled = true;
- //btn_stop.Enabled = false;
- //btn_sendsound.Enabled = true;
- //btn_play.Enabled = true;
- if (waveIn != null)
- waveIn.StopRecording();
- //Cleanup();
- }
- private void Cleanup()
- {
- if (waveIn != null)
- {
- waveIn.Dispose();
- waveIn = null;
- }
- FinalizeWaveFile();
- }
- private void FinalizeWaveFile()
- {
- if (writer != null)
- {
- writer.Dispose();
- writer = null;
- }
- }
- //开始录音
- private void btn_luyin_Click(object sender, EventArgs e)
- {
- btn_stop.Enabled = true;
- btn_luyin.Enabled = false;
- if (waveIn == null)
- {
- CreateWaveInDevice();
- }
- if (File.Exists(soundfile))
- {
- File.Delete(soundfile);
- }
- writer = new WaveFileWriter(soundfile, waveIn.WaveFormat);
- waveIn.StartRecording();
- }
上面的代码实现了录音,并且写入文件p2psound_A.wav

语音发送
获取到语音后我们要把语音发送出去
当我们录好音后点击发送,这部分相关代码是
- MsgTranslator tran = null;
- ublic Form1()
- {
- InitializeComponent();
- LoadWasapiDevicesCombo();//显示音频设备
- Config cfg = SeiClient.GetDefaultConfig();
- cfg.Port = 7777;
- UDPThread udp = new UDPThread(cfg);
- tran = new MsgTranslator(udp, cfg);
- tran.MessageReceived += tran_MessageReceived;
- tran.Debuged += new EventHandler<DebugEventArgs>(tran_Debuged);
- }
- private void btn_sendsound_Click(object sender, EventArgs e)
- {
- if (t_ip.Text == "")
- {
- MessageBox.Show("请输入ip");
- return;
- }
- if (t_port.Text == "")
- {
- MessageBox.Show("请输入端口号");
- return;
- }
- string ip = t_ip.Text;
- int port = int.Parse(t_port.Text);
- string nick = t_nick.Text;
- string msg = "语音消息";
- IPEndPoint remote = new IPEndPoint(IPAddress.Parse(ip), port);
- Msg m = new Msg(remote, "zz", nick, Commands.SendMsg, msg, "Come From A");
- m.IsRequireReceive = true;
- m.ExtendMessageBytes = FileContent(soundfile);
- m.PackageNo = Msg.GetRandomNumber();
- m.Type = Consts.MESSAGE_BINARY;
- tran.Send(m);
- }
- private byte[] FileContent(string fileName)
- {
- FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
- try
- {
- byte[] buffur = new byte[fs.Length];
- fs.Read(buffur, 0, (int)fs.Length);
- return buffur;
- }
- catch (Exception ex)
- {
- return null;
- }
- finally
- {
- if (fs != null)
- {
- //关闭资源
- fs.Close();
- }
- }
- }
如此一来我们就把产生的语音文件发送出去了
语音的接收与播放
其实语音的接收和文本消息的接收没有什么不同,只不过语音发送的时候是以二进制发送的,因此我们在收到语音后 就应该写入到一个文件里面去,接收完成后,播放这段语音就行了。
下面这段代码主要是把收到的数据保存到文件中去,这个函数式我的NetFrame里收到消息时所触发的事件,在文章前面提过的那篇文章里
- void tran_MessageReceived(object sender, MessageEventArgs e)
- {
- Msg msg = e.msg;
- if (msg.Type == Consts.MESSAGE_BINARY)
- {
- string m = msg.Type + "->" + msg.UserName + "发来二进制消息!";
- AddServerMessage(m);
- if (File.Exists(recive_soundfile))
- {
- File.Delete(recive_soundfile);
- }
- FileStream fs = new FileStream(recive_soundfile, FileMode.Create, FileAccess.Write);
- fs.Write(msg.ExtendMessageBytes, 0, msg.ExtendMessageBytes.Length);
- fs.Close();
- //play_sound(recive_soundfile);
- ChangeBtn(true);
- }
- else
- {
- string m = msg.Type + "->" + msg.UserName + "说:" + msg.NormalMsg;
- AddServerMessage(m);
- }
- }
收到语音消息后,我们要进行播放,播放时仍然用刚才那个插件播放
- //--------播放部分----------
- private IWavePlayer wavePlayer;
- private WaveStream reader;
- public void play_sound(string filename)
- {
- if (wavePlayer != null)
- {
- wavePlayer.Dispose();
- wavePlayer = null;
- }
- if (reader != null)
- {
- reader.Dispose();
- }
- reader = new MediaFoundationReader(filename, new MediaFoundationReader.MediaFoundationReaderSettings() { SingleReaderObject = true });
- if (wavePlayer == null)
- {
- wavePlayer = new WaveOut();
- wavePlayer.PlaybackStopped += WavePlayerOnPlaybackStopped;
- wavePlayer.Init(reader);
- }
- wavePlayer.Play();
- }
- private void WavePlayerOnPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs)
- {
- if (stoppedEventArgs.Exception != null)
- {
- MessageBox.Show(stoppedEventArgs.Exception.Message);
- }
- if (wavePlayer != null)
- {
- wavePlayer.Stop();
- }
- btn_luyin.Enabled = true;
- }private void btn_play_Click(object sender, EventArgs e)
- {
- btn_luyin.Enabled = false;
- play_sound(soundfile);
- }


在上面演示了接收和发送一段语音消息的界面
技术总结
主要用到的技术就是UDP和NAudio的录音和播放功能
其中用到的UDP传输类我放在了github上面 地址在我的博客左边的个人介绍里有地址 项目地址 https://github.com/zhujunxxxxx/ZZNetFrame
希望这篇文章能够提供一个思路。
C#基于UDP实现的P2P语音聊天工具(1)的更多相关文章
- c#基于udp实现的p2p语音聊天工具
原创性申明 此博文的出处 为 http://blog.csdn.net/zhujunxxxxx/article/details/40124773假设进行转载请注明出处.本文作者原创,邮箱zhujunx ...
- Android 即时语音聊天工具 开发
使用融云SDK 1. 功能需求分析 1.1 核心功能需求: * 即时通讯 * 文字聊天 * 语音聊天 1.2 辅助功能需求: * 注册.登录 * 好友添加功能 * 好友关系管理 2. 融云即时通讯平台 ...
- 基于Nodejs开发的web即时聊天工具
由于公司需要开发web即时聊天的功能,开始时我们主要的实施方法是用jquery的ajax定时(10秒)轮询向服务器请求,由于是轮询请求,对 服务器的压力比较大.我们网站上线的时间不长,访问量不是很大, ...
- C 基于UDP实现一个简易的聊天室
引言 本文是围绕Linux udp api 构建一个简易的多人聊天室.重点看思路,帮助我们加深 对udp开发中一些api了解.相对而言udp socket开发相比tcp socket开发注意的细节要少 ...
- C++开发的基于TCP协议的内网聊天工具
项目相关地址 源码:https://github.com/easonjim/TCPChat bug提交:https://github.com/easonjim/TCPChat/issues
- Pilin —— 一个基于Xmpp openfire smack的即时聊天工具
https://github.com/whfcomm/Pilin
- 基于Qt的P2P局域网聊天及文件传送软件设计
基于Qt的P2P局域网聊天及文件传送软件设计 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<通信网络>的课程设计作业,之 ...
- 基于UDP协议的控制台聊天
这几天学了java的网络编程弄出一个基于UDP协议的聊天工具 功能 添加并且备注好友(输入对方的ip) 删除好友 查看好友列表 用java写的控制台程序导出可执行程序后不能双击打开 还需要些一个脚本文 ...
- 与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室
原文:与众不同 windows phone (31) - Communication(通信)之基于 Socket UDP 开发一个多人聊天室 [索引页][源码下载] 与众不同 windows phon ...
随机推荐
- Web Service 初步了解
Web Service见名之意就是网络上的一些服务,解决的问题就是如何使用这些服务,因为软件的开发有各种各样的语言,利用Java,C#,VB.NET,PHP等等,如何使这些语言编写的程序能够进行互通, ...
- (转)A drop-in universal solution for moving text fields out of the way of the keyboard
There are a hundred and one proposed solutions out there for how to move UITextField andUITextView o ...
- (转)Android中截取当前屏幕图片
该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面.实现的代码如下: /** * 获取和保存当前屏幕的截图 */ priv ...
- core_cm3文件函数一览
core_cm3是ARM公司推出来的统一规定,这是对下游芯片厂商的统一规定,因此可以再Cortex-M3(CM3)之间进行移植.此文件中定义了一些对特殊功能寄存器的C语言形式的操作,本质上是内敛汇编和 ...
- 执行eclipse,迅速failed to create the java virtual machine。
它们必须在一排,否则会出现The Eclipse executable launcher was unable to locate its companion shared library的错误 打开 ...
- git config配置文件 (共有三个配置文件)
设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...
- transition过渡的趣玩
本例中将三张图(来自网络)进行堆叠,鼠标悬停触发.附有源代码
- eclipse 404以及tomcat failed to start错误
eclipse中的servlet项目有时会不编译,不编译可能就会出现404错误,因为在build path的输出目录并没有class文件,然而如果在输出目录引入之前编译的class文件,就可能出现cl ...
- C#中Property和Attribute的区别
C#中Property和Attribute的区别 Attribute 字段Property 属性(get;set;) 属性的正常写: private string name; public strin ...
- python字符串的encode和decode
原文 decode的作用是将其他编码的字符串转换成unicode编码. str1.decode('gb2312') #表示将gb2312编码的字符串转换成unicode编码 encode的作用是将un ...