前一篇文章中实现了文字聊天和共享白板的功能,这篇文章中,我将在前一篇文章的基础上实现语音聊天的功能。语音聊天要比文字聊天和共享白板难度要大一点。
实现的大概的流程为:
1、一个聊天室成员向另外一个成员发起语音聊天请求
2、这个请求将被送至WCF服务端,WCF的双工通知被邀请人。
3、被邀请人接到通知,他可以选择接受或者拒绝语音聊天的请求。
4、如果拒绝,将通知请求者拒绝语音聊天
5、如果同意,邀请者和被邀请者的客户端将进行语音聊天,此时客户端会开启一个播放声音和接受声音的线程。这里用到了一个开源的wave类库,在http://www.lumisoft.ee/lswww/download/downloads/Examples/可以下载。声音的通信使用到了UDPClient
类。这个类使用 UDP 与网络服务通讯。UDP 的优点是简单易用,并且能够同时向多个地址广播消息。UdpClient 类提供了一些简单的方法,用于在阻止同步模式下发送和接收无连接 UDP 数据报。因为 UDP 是无连接传输协议,所以不需要在发送和接收数据前建立远程主机连接。但您可以选择使用下面两种方法之一来建立默认远程主机:
可以使用在UdpClient 中提供的任何一种发送方法将数据发送到远程设备。使用
Receive 方法可以从远程主机接收数据。
这篇文章使用了Receive 方法从客户端接受数据。然后通过WCF中存储的IP地址,通过Send方法将其发送给客户端。
下面我将在前一篇文章的基础上实现这个语音聊天的功能。首先在客户端添加声音管理的类CallManager,这个类使用到了开源的wave类库,代码如下:
public class CallManager
{
private WaveIn _waveIn;
private WaveOut _waveOut;
private IPEndPoint _serverEndPoint;
private Thread _playSound;
private UdpClient _socket;
public CallManager(IPEndPoint serverEndpoint)
{
_serverEndPoint = serverEndpoint;
}
public void Start()
{
if (_waveIn != null || _waveOut != null)
{
throw new Exception("Call is allready started");
}
int waveInDevice = (Int32)Application.UserAppDataRegistry.GetValue("WaveIn", 0);
int waveOutDevice = (Int32)Application.UserAppDataRegistry.GetValue("WaveOut", 0);
_socket = new UdpClient(0); // opens a random available port on all interfaces
_waveIn = new WaveIn(WaveIn.Devices[waveInDevice], 8000, 16, 1, 400);
_waveIn.BufferFull += new BufferFullHandler(_waveIn_BufferFull);
_waveIn.Start();
_waveOut = new WaveOut(WaveOut.Devices[waveOutDevice], 8000, 16, 1);
_playSound = new Thread(new ThreadStart(playSound));
_playSound.IsBackground = true;
_playSound.Start();
}
private void playSound()
{
try
{
while (true)
{
lock (_socket)
{
if (_socket.Available != 0)
{
IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
byte[] received = _socket.Receive(ref endpoint);
// todo: add codec
_waveOut.Play(received, 0, received.Length);
}
}
Thread.Sleep(1);
}
}
catch (ThreadAbortException)
{
}
catch
{
this.Stop();
}
}
void _waveIn_BufferFull(byte[] buffer)
{
lock (_socket)
{
//todo: add codec
_socket.Send(buffer, buffer.Length, _serverEndPoint);
}
}
public void Stop()
{
if (_waveIn != null)
{
_waveIn.Dispose();
}
if (_waveOut != null)
{
_waveOut.Dispose();
}
if (_playSound.IsAlive)
{
_playSound.Abort();
}
if (_socket != null)
{
_socket.Close();
_socket = null;
}
}
}
在服务端添加将接受到的声音,发送给接受者的类,使用到了UDPClient类:
public class UdpServer
{
private Thread _listenerThread;
private List<IPEndPoint> _users = new List<IPEndPoint>();
private UdpClient _udpSender = new UdpClient();
public IPAddress ServerAddress
{
get;
set;
}
public UdpClient UdpListener
{
get;
set;
}
public UdpServer()
{
try
{
ServerAddress = IPAddress.Parse("127.0.0.1");
}
catch
{
throw new Exception("Configuration not set propperly. View original source code");
}
}
public void Start()
{
UdpListener = new System.Net.Sockets.UdpClient(0);
_listenerThread = new Thread(new ThreadStart(listen));
_listenerThread.IsBackground = true;
_listenerThread.Start();
}
private void listen()
{
while (true)
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
byte[] received = UdpListener.Receive(ref sender);
if (!_users.Contains(sender))
{
_users.Add(sender);
}
foreach (IPEndPoint endpoint in _users)
{
if (!endpoint.Equals(sender))
{
_udpSender.Send(received, received.Length, endpoint);
}
}
}
}
public void EndCall()
{
_listenerThread.Abort();
}
}
在WCF服务中添加两个方法:初始化语音通信和结束语音通信。
[OperationContract(IsOneWay = false)]
bool InitiateCall(string username);
[OperationContract(IsOneWay = true)]
void EndCall();
具体是实现代码:
public bool InitiateCall(string username)
{
ClientCallBack clientCaller = s_dictCallbackToUser[OperationContext.Current.GetCallbackChannel<IZqlChartServiceCallback>()];
ClientCallBack clientCalee = s_dictCallbackToUser.Values.Where(p => p.JoinChatUser.NickName == username).First();
if (clientCaller.Callee != null || clientCalee.Callee != null) // callee or caller is in another call
{
return false;
}
if (clientCaller == clientCalee)
{
return false;
}
if (clientCalee.Client.AcceptCall(clientCaller.JoinChatUser.NickName))
{
clientCaller.Callee = clientCalee.Client;
clientCalee.Callee = clientCaller.Client;
clientCaller.UdpCallServer = new UdpServer();
clientCaller.UdpCallServer.Start();
EmtpyDelegate separateThread = delegate()
{
IPEndPoint endpoint = new IPEndPoint(clientCaller.UdpCallServer.ServerAddress,
((IPEndPoint)clientCaller.UdpCallServer.UdpListener.Client.LocalEndPoint).Port);
clientCalee.Client.CallDetailes(endpoint, clientCaller.JoinChatUser.NickName, username);
clientCaller.Client.CallDetailes(endpoint, clientCaller.JoinChatUser.NickName, username);
foreach (var callback in s_dictCallbackToUser.Keys)
{
callback.NotifyMessage(String.Format("System:User \"{0}\" and user \"{1}\" have started a call",clientCaller.JoinChatUser.NickName, username));
}
};
separateThread.BeginInvoke(null, null);
return true;
}
else
{
return false;
}
}
public void EndCall()
{
ClientCallBack clientCaller = s_dictCallbackToUser[OperationContext.Current.GetCallbackChannel<IZqlChartServiceCallback>()];
ClientCallBack ClientCalee = s_dictCallbackToUser[clientCaller.Callee];
if (clientCaller.UdpCallServer != null)
{
clientCaller.UdpCallServer.EndCall();
}
if (ClientCalee.UdpCallServer != null)
{
ClientCalee.UdpCallServer.EndCall();
}
if (clientCaller.Callee != null)
{
foreach (var callback in s_dictCallbackToUser.Keys)
{
callback.NotifyMessage(String.Format("System:User \"{0}\" and user \"{1}\" have ended the call", clientCaller.JoinChatUser.NickName, ClientCalee.JoinChatUser.NickName));
}
clientCaller.Callee.EndCallClient();
clientCaller.Callee = null;
}
if (ClientCalee.Callee != null)
{
ClientCalee.Callee.EndCallClient();
ClientCalee.Callee = null;
}
}
还有部分做了修改的代码见附件代码。
下面看下演示的截图:
1、两个用户登录:
2、选择小花,点击按钮。麒麟向小花同学发起语音聊天:
3、小花同学接受通知,选择是:
4、弹出通话中的窗体,双发都可以选择结束通话:
5、结束通话之后,消息框中会广告消息
总结:
这个聊天程序主要都是用到了WCF的双工通信。没有用两台机子测试,我在我的笔记本上开了一个服务端和一个客户端,用了一个带耳麦的耳机,声音效果良好。
其实这个东西做完整还有很多细活,这个只是Demo,非常的粗糙。最近会很忙,期待将来的某一天能空去完善。如果实现了视频的功能我会写第四篇的。
代码:http://files.cnblogs.com/zhuqil/Zql_src.rar
分享到:
相关推荐
WPF+WCF一步一步打造音频聊天室,里面还带有一个说明文档。
wpf, wcf 的简单程序示例,适合首次学习wpf和wcf技术的菜鸟们。
wpf
WPF + WCF画板聊天程序,即时通讯
wpf+wcf+ef搭建的框架,初学者适用
ASP+WPF+WCF,包含ASP资料、WPF示例、WCF示例,具体内容自己研究好了
此示例程序中集成了WPF、WCF、WWF和LINQ,是学习.net 30.和.net 3.5的较好的范例
WPF+WCF做的大文件上传控件,在WPF客户端把大文件分割成二进制数组,每次往服务器上写100K,可断点续传。页面做的比较丑,没认真设计,只为实现功能。另外还有一个Silverlight+WCF版的。
通讯用的WCF,客户端用WPF写的一个聊天Demo
一个WPF+WCF的例子,在WPF很少的情况下,学习学习。本不想收分的,但我也想下其它的东东,所以。。。
EF+WCF+WPF(MVVM)框架简单示例
一个学习WCF+LINQ+WPF很好的一个范例
WCF & WPF 聊天程序源码 For those that have read some of my other CodeProject articles you will probably know, that I am not shy about trying out new technologies. But one good thing about that is ...
运用wpf wcf 实现的聊天室的功能,非常适合学习wcf的
WPF、WCF、WF打造Hello World程序
最简单的WPF+MVVM项目最简单的WPF+MVVM项目最简单的WPF+MVVM项目最简单的WPF+MVVM项目
wpf+wcf写的聊天室。大家可以下载看看。,值得学习。
WPF+EF+SQLite从建项目到写入数据开发环境配置 运行环境:VS2019 数据库工具:sqlitestudio 用于验证数据是否插入,项目中的数据库就是这个工具创建的 操作流程: 新建项目=> 安装System.Data.SQLite=> 配置App....
C# WPF+MVVM+EF+多语言实例源码
C#+WPF+Prism模块化开发示例。本示例简述了Prism框架的基本应用,包括数据绑定,事件等内容