c# 实现语音聊天的实战示例
作者:yswenli 时间:2021-11-18 06:29:20
一、语音聊天说专业点就是即时语音,是一种基于网络的快速传递语音信息的技术,普遍应用于各类社交软件中,优势主要有以下几点:
(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
如果发现本文有什么问题和任何建议,也随时欢迎交流~
来源:https://www.cnblogs.com/yswenli/p/14353482.html
标签:c#,语音,聊天
0
投稿
猜你喜欢
Java包装类的概述与应用
2022-03-14 07:50:43
Android实现截屏方式整理(总结)
2023-12-07 05:10:18
C#微信公众号开发 微信事件交互
2023-04-22 21:18:31
阿里开源Java诊断工具神器使用及场景详解
2023-11-06 17:24:21
Kotlin与Java的区别详解
2023-06-22 16:25:58
Java中的内部类使用详情
2022-07-24 05:09:38
详解spring boot引入外部jar包的坑
2021-07-29 10:49:49
idea中的Maven导包失败问题解决方案汇总
2023-07-12 12:34:22
linux环境下java程序打包成简单的hello world输出jar包示例
2023-11-26 11:11:37
java信号量控制线程打印顺序的示例分享
2023-05-09 12:27:38
解决Spring Batch框架job任务只跑一次的问题
2023-01-07 00:13:53
浅谈Spring Cloud Ribbon的原理
2023-07-23 04:11:25
SpringMVC @NotNull校验不生效的解决方案
2021-07-19 23:20:07
springboot操作静态资源文件的方法
2022-07-13 06:29:11
Mybatis-Plus注入SQL原理分析
2022-11-09 21:17:22
Java异常处理操作 Throwable、Exception、Error
2022-02-19 20:56:13
java interface的两个经典用法
2021-08-17 06:20:56
关于synchronized有趣的同步问题
2021-11-03 05:46:55
详解java动态代理模式
2023-03-29 13:39:29
分析Java中为什么String不可变
2023-06-05 23:33:25