c# 实现语音聊天的实战示例
一、语音聊天说专业点就是即时语音,是一种基于网络的快速传递语音信息的技术,普遍应用于各类社交软件中,优势主要有以下几点:
(1)时效性:视频直播会因为带宽问题有时出现延迟高的问题,而语音直播相对来说会好很多,延迟低,并且能够第?一时间与听众互动,时效性强。
(2)隐私性:这一点体现在何处,如主播不想暴露自己的长相,或者进行问题回答是,没有视频的话会让主播感到更安心,所以语音直播隐私性更强。
(3)内容质量高:因为语音直播不靠“颜值”只有好的内容才能够吸引用户,所以语音直播相对来说内容质量更高。
(4)成本降低:语音直播相对视频直播来说,带宽流量等都会便宜许多,成本降低不少,更加实惠。
二、语音聊天主要步骤:音频采集、压缩编码、网络传输、解码还原、播放音频,如下图所示
下面就从代码的角度来详说一下这几个步骤。
(1)音频采集,读取麦克风设备数据
private readonly WaveIn _waveIn; _waveIn = new WaveIn(); _waveIn.BufferMilliseconds = 50; _waveIn.DeviceNumber = 0; _waveIn.DataAvailable += OnAudioCaptured; _waveIn.StartRecording();
(2)音频数据压缩编码,常见压缩格式比较多,例如mp3、acc、speex等,这里以speex为例
private readonly WideBandSpeexCodec _speexCodec; _speexCodec = new WideBandSpeexCodec(); _waveIn.WaveFormat = _speexCodec.RecordFormat; void OnAudioCaptured(object sender, WaveInEventArgs e) { byte[] encoded = _speexCodec.Encode(e.Buffer, 0, e.BytesRecorded); _audioClient.Send(encoded); }
(3)网络传输,为了保证即时传输udp协议有着天然的优点
using SAEA.Sockets; using SAEA.Sockets.Base; using SAEA.Sockets.Model; using System; using System.Net; namespace GFF.Component.GAudio.Net { public class AudioClient { IClientSocket _udpClient; BaseUnpacker _baseUnpacker; public event Action<Byte[]> OnReceive; public AudioClient(IPEndPoint endPoint) { var bContext = new BaseContext(); _udpClient = SocketFactory.CreateClientSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) .SetIPEndPoint(endPoint) .UseIocp(bContext) .SetReadBufferSize(SocketOption.UDPMaxLength) .SetWriteBufferSize(SocketOption.UDPMaxLength) .Build()); _baseUnpacker = (BaseUnpacker)bContext.Unpacker; _udpClient.OnReceive += _udpClient_OnReceive; } private void _udpClient_OnReceive(byte[] data) { OnReceive?.Invoke(data); } public void Connect() { _udpClient.Connect(); } public void Send(byte[] data) { _udpClient.SendAsync(data); } public void Disconnect() { _udpClient.Disconnect(); } } }
(4)服务器转发,客户端使用udp,服务器这里同样也使用udp来转发
using SAEA.Sockets; using SAEA.Sockets.Base; using SAEA.Sockets.Interface; using SAEA.Sockets.Model; using System; using System.Collections.Concurrent; using System.Net; using System.Threading.Tasks; namespace GFF.Component.GAudio.Net { public class AudioServer { IServerSocket _udpServer; ConcurrentDictionary<string, IUserToken> _cache; public AudioServer(IPEndPoint endPoint) { _cache = new ConcurrentDictionary<string, IUserToken>(); _udpServer = SocketFactory.CreateServerSocket(SocketOptionBuilder.Instance.SetSocket(SAEASocketType.Udp) .SetIPEndPoint(endPoint) .UseIocp<BaseContext>() .SetReadBufferSize(SocketOption.UDPMaxLength) .SetWriteBufferSize(SocketOption.UDPMaxLength) .SetTimeOut(5000) .Build()); _udpServer.OnAccepted += _udpServer_OnAccepted; _udpServer.OnDisconnected += _udpServer_OnDisconnected; _udpServer.OnReceive += _udpServer_OnReceive; } public void Start() { _udpServer.Start(); } public void Stop() { _udpServer.Stop(); } private void _udpServer_OnReceive(ISession currentSession, byte[] data) { Parallel.ForEach(_cache.Keys, (id) => { try { _udpServer.SendAsync(id, data); } catch { } }); } private void _udpServer_OnAccepted(object obj) { var ut = (IUserToken)obj; if (ut != null) { _cache.TryAdd(ut.ID, ut); } } private void _udpServer_OnDisconnected(string ID, Exception ex) { _cache.TryRemove(ID, out IUserToken _); } } }
(5)解码还原,客户端将从服务器收到的数据按约定的压缩格式,进行解压缩还原成音频数据
private readonly BufferedWaveProvider _waveProvider; _waveProvider = new BufferedWaveProvider(_speexCodec.RecordFormat); private void _audioClient_OnReceive(byte[] data) { byte[] decoded = _speexCodec.Decode(data, 0, data.Length); _waveProvider.AddSamples(decoded, 0, decoded.Length); }
(6)播放音频,使用播放设备来播放解码后的音频数据
private readonly IWavePlayer _waveOut; _waveOut = new WaveOut(); _waveOut.Init(_waveProvider); _waveOut.Play();
三、测试运行,通过分析语音聊天的几个关键问题点后,按步骤封装好代码,接下来就是用实例来测试一下效果了。
客户端封装在按钮事件中:
GAudioClient _gAudioClient = null; private void toolStripDropDownButton2_ButtonClick(object sender, EventArgs e) { if (_gAudioClient == null) { ClientConfig clientConfig = ClientConfig.Instance(); _gAudioClient = new GAudioClient(clientConfig.IP, clientConfig.Port + 2); _gAudioClient.Start(); } else { _gAudioClient.Dispose(); _gAudioClient = null; } }
服务端封装在main函数中:
ConsoleHelper.WriteLine("正在初始化语音服务器...", ConsoleColor.DarkBlue); _gAudioServer = new GAudioServer(filePort + 1); ConsoleHelper.WriteLine("语音服务器初始化完毕...", ConsoleColor.DarkBlue); ConsoleHelper.WriteLine("正在启动语音服务器...", ConsoleColor.DarkBlue); _gAudioServer.Start(); ConsoleHelper.WriteLine("语音服务器初始化完毕", ConsoleColor.DarkBlue);
万事俱备,现在F5跑起来试试。
如上红框所示,喊了几句相当于Hello World的Hello没有问题,大功初步告成~
原文作者:https://www.cnblogs.com/yswenli/p/14353482.html
更多内容欢迎我的的github:https://github.com/yswenli/GFF
如果发现本文有什么问题和任何建议,也随时欢迎交流~