实现一个简单的语音聊天室(源码)

实现⼀个简单的语⾳聊天室(源码
 语⾳聊天室,或多⼈语⾳聊天,是即时通信应⽤中常见的功能之⼀,⽐如,QQ的语⾳讨论组就是我们⽤得⽐较多的。
这篇⽂章将实现⼀个简单的语⾳聊天室,让多个⼈可以进⼊同⼀个房间进⾏语⾳沟通。先看运⾏效果截图:
  从左到右的三张图分别是:登录界⾯、语⾳聊天室的主界⾯、标注了各个控件的主界⾯。
  (如果觉得界⾯太丑,没关系,后⾯下载源码后,你可以⾃⼰美化~~)
⼀. C/S结构
  很明显,我这个语⾳聊天室采⽤的是C/S结构,整个项⽬结构相对⽐较简单,如下所⽰:
   该项⽬的底层是基于构建的。这样,服务端就基本没写代码,直接把OMCS服务端拿过来⽤;客户端就⽐较⿇烦些,下⾯我就重点讲客户端的开发。
⼆. 客户端控件式开发
  客户端开发了多个⾃定义控件,然后将它们组装到⼀起,以完成语⾳聊天室的功能。为了便于讲解,我主界⾯的图做了标注,以指⽰出各个⾃定义控件。 
  现在我们分别介绍各个控件:
1. 分贝显⽰器
  分贝显⽰器⽤于显⽰声⾳的⼤⼩,⽐如麦克风采集到的声⾳的⼤⼩,或扬声器播放的声⾳的⼤⼩。如上图中3标注的。
(1)傅⽴叶变换
  将声⾳数据转换成分贝强度使⽤的是傅⽴叶变换。其对应的是客户端项⽬中的FourierTransformer静态类。源码⽐较简单,就不贴出来了,⼤家⾃⼰去看。细分驱动器
  DecibelDisplayer 使⽤的是PrograssBar来显⽰声⾳强度的⼤⼩。
  每当有声⾳数据交给DecibelDisplayer显⽰时,⾸先,DecibelDisplayer会调⽤上⾯的傅⽴叶变换将其转换为分贝,然后,将其映射为PrograssBar的对应的Value。
2.发⾔者控件 SpeakerPanel
  SpeakerPanel ⽤于表⽰聊天室中的⼀个成员,如上图中1所⽰。它显⽰了成员的ID,成员的声⾳的强度(使⽤DecibelDisplayer控件),以及其麦克风的状态(启⽤、引⽤)。
  这个控件很重要,我将其源码贴出来:
public partial class SpeakerPanel : UserControl ,IDisposable
{
卡环弯制private ChatUnit chatUnit;
gsm模块public SpeakerPanel()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);//调整⼤⼩时重绘
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);// 禁⽌擦除背景.
this.SetStyle(ControlStyles.UserPaint, true);//⾃⾏绘制
this.UpdateStyles();
}
public string MemberID
{
get
焊锰钢板用什么焊条{
if (this.chatUnit == null)
{
return null;
}
return this.chatUnit.MemberID;
}
}
public void Initialize(ChatUnit unit)
{
this.chatUnit = unit;
this.skinLabel_name.Text = unit.MemberID;
this.chatUnit.MicrophoneConnector.ConnectEnded += new CbGeneric<OMCS.Passive.ConnectRes
ult>(MicrophoneConnector_ConnectEnded);            this.chatUnit.MicrophoneConnector.OwnerOutputChanged += new CbGeneric(MicrophoneConnector_OwnerOutputChanged);
this.chatUnit.MicrophoneConnector.AudioDataReceived += new CbGeneric<byte[]>(MicrophoneConnector_AudioDataReceived);
this.chatUnit.MicrophoneConnector.BeginConnect(unit.MemberID);
}
public void Initialize(string curUserID)
{
this.skinLabel_name.Text = curUserID;
this.skinLabel_name.ForeColor = Color.Red;
this.pictureBox_Mic.Visible = false;
this.decibelDisplayer1.Visible = false;
}
void MicrophoneConnector_AudioDataReceived(byte[] data)
{
this.decibelDisplayer1.DisplayAudioData(data);
}
if (this.InvokeRequired)
{
this.BeginInvoke(new CbGeneric(this.MicrophoneConnector_OwnerOutputChanged));
}
else
{
this.ShowMicState();
}
}
private ConnectResult connectResult;
void MicrophoneConnector_ConnectEnded(ConnectResult res)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbGeneric<ConnectResult>(this.MicrophoneConnector_ConnectEnded), res);            }
else
{
this.ShowMicState();
}
}
public void Dispose()
{
this.chatUnit.Close();
}
private void ShowMicState()
{
if (tResult != OMCS.Passive.ConnectResult.Succeed)
{
this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[2];
}
else
{
this.decibelDisplayer1.Working = false;
if (!this.chatUnit.MicrophoneConnector.OwnerOutput)
{
this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[1];
return;
}
if (this.chatUnit.MicrophoneConnector.Mute)
{
this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[1];
}
else
{
this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[0];
this.decibelDisplayer1.Working = true;
}
}
}
private void pictureBox_Mic_Click(object sender, EventArgs e)
{
return;
}
this.chatUnit.MicrophoneConnector.Mute = !this.chatUnit.MicrophoneConnector.Mute;
this.ShowMicState();
}
}
(1)在代码中,ChatUnit就代表当前这个聊天室中的成员。我们使⽤其MicrophoneConnector连接到⽬标成员的麦克风。
(2)预定MicrophoneConnector的AudioDataReceived事件,当收到语⾳数据时,将其交给DecibelDisplayer去显⽰声⾳的⼤⼩。
(3)预定MicrophoneConnector的ConnectEnded和OwnerOutputChanged事件,根据其结果来显⽰SpeakerPanel空间上麦克风图标的状态(对应ShowMicState⽅法)。
3. MultiAudioChatContainer 控件
  MultiAudioChatContainer对应上图中2标注的控件,它主要做了以下⼏件事情:
(1)在初始化时,加⼊聊天室:通过调⽤IMultimediaManager的ChatGroupEntrance属性的Join⽅法。
法兰锻造(2)使⽤FlowLayoutPanel将聊天室中每个成员对应的SpeakerPanel罗列出来。
(3)当有成员加⼊或退出聊天室时(对应ChatGroup的SomeoneJoin和SomeoneExit事件),动态添加或移除对应的SpeakerPanel实例。自动化物流线
(4)通过CheckBox将⾃⼰设备(麦克风和扬声器)的控制权暴露出来。我们可以启⽤或禁⽤我们⾃⼰的麦克风或扬声器。
(5)注意,其提供了Close⽅法,这意味着,在关闭包含了该控件的宿主窗体时,要调⽤其Close⽅法以释放其内部持有的麦克风连接器等资源。
  在完成MultiAudioChatContainer后,我们这个聊天室的核⼼就差不多了。接下来就是弄个主窗体,然后把MultiAudioChatContainer拖上去,初始化IMultimediaManager,并传递给MultiAudioChatContainer就⼤功告成了。
三. 源码下载
  上⾯只是讲了实现多⼈语⾳聊天室中的⼏个重点,并不全⾯,⼤家下载下⾯的源码可以更深⼊的研究。
  最后,跟⼤家说说部署的步骤:
(1)将服务端部署在⼀台机器上,启动服务端。
(2)修改客户端配置⽂件中的ServerIP为刚才服务器的IP。
(3)在多台机器上运⾏客户端,以不同的帐号登录到同⼀个房间(如默认的R1000)。
(4)如此,多个⽤户就处于同⼀个聊天室进⾏语⾳聊天了。

本文发布于:2024-09-22 23:23:57,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/148214.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:对应   控件   客户端   源码   数据   成员   事件
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议