首页 > 编程知识 正文

语音聊天室的app,简易网站源码

时间:2023-05-04 08:30:21 阅读:140744 作者:223

聊天室或多人语音聊天是即时消息收发中的常见功能之一,例如QQ的语音讨论组我们经常使用。

本文实现了一个简单的语音聊天室,多人可以进入同一个房间进行语音交流。 首先看执行效果的截图:

从左到右的三幅图是登录界面、语音聊天室的主界面、显示各控件的主界面。

(如果你觉得界面丑陋,没关系。 您可以稍后下载源代码,然后自己美化它~~) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )

一. C/S结构很明显,我这个语音聊天室采用C/S结构,整个项目结构比较简单,如下。

该项目的基础是根据OMCS建立的。 这样,服务器端很少编写代码,直接把OMCS服务器端带来使用的客户端很麻烦,所以将重点放在客户端的开发上。

2 .客户端控件式开发客户端开发了多个自定义控件,将它们结合起来完成语音聊天室的功能。 为了便于说明,我在我的主界面中对图进行了标记,并展示了每个自定义控件。

现在,我们将讨论每个控件。

1 .分贝显示器分贝显示器用于显示声音的大小,包括麦克风采集到的声音大小和扬声器播放的声音大小。 标记为图中3

(1)傅立叶变换

将语音数据变换为分贝强度使用傅立叶变换。 它对应于客户机项目的FourierTransformer静态类。 源代码很简单,所以不投稿,大家一起去看。

)2)声音强度显示控件DecibelDisplayer

DecibelDisplayer使用程序栏显示声音强度的大小。

每次音频数据传递到DecibelDisplayer并显示时,DecibelDisplayer首先调用上面的傅立叶变换将其转换为分贝,然后将其映射到程序栏中的相应值。

2 .发言者控件SpeakerPanel SpeakerPanel用于表示聊天室成员。 如上述图中1所示。 它显示了成员的ID、成员的声音强度(使用DecibelDisplayer控件)及其麦克风的状态(已启用,请参见)。

这个控制很重要。 粘贴源代码。

publicpartialclassspeakerpanel :用户控制,idisposable { privatechatunitchatunit; publicspeakerpanel ((初始化组件) ); this.set style (control styles.resize redraw,true ); //调整大小时this.set style (control styles.optimizeddoublebuffer,true ); //双缓冲区this.set style (control styles.allpaintinginwmpaint,true ); //禁止背景擦除. this.set style (control styles.user paint,true ); //自己绘制this.UpdateStyles (); }公共string memberid { get { if } this.chat unit==null } { return null; } return this.chatUnit.MemberID; } public void initialize (chatunit ) { this.chatUnit=unit; this.skin label _ name.text=unit.memberid; this.chat unit.microphone connector.connectended=newcbgenericomcs.passive.connect result (microphone connector _ coner _ con ) owneroutputchanged=newcbgeneric (microphone connector _ 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); } void MicrophoneConnector_OwnerOutputChanged() { 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.connectResult = res; this.ShowMicState(); } } public void Dispose() { this.chatUnit.Close(); } private void ShowMicState() { if (this.connectResult != OMCS.Passive.ConnectResult.Succeed) { this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[2]; this.toolTip1.SetToolTip(this.pictureBox_Mic, this.connectResult.ToString()); } else { this.decibelDisplayer1.Working = false; if (!this.chatUnit.MicrophoneConnector.OwnerOutput) { this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[1]; this.toolTip1.SetToolTip(this.pictureBox_Mic, "好友禁用了麦克风"); return; } if (this.chatUnit.MicrophoneConnector.Mute) { this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[1]; this.toolTip1.SetToolTip(this.pictureBox_Mic, "静音"); } else { this.pictureBox_Mic.BackgroundImage = this.imageList1.Images[0]; this.toolTip1.SetToolTip(this.pictureBox_Mic, "正常"); this.decibelDisplayer1.Working = true; } } } private void pictureBox_Mic_Click(object sender, EventArgs e) { if (!this.chatUnit.MicrophoneConnector.OwnerOutput) { 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就大功告成了。

三. 源码下载

  上面只是讲了实现多人语音聊天室中的几个重点,并不全面,大家下载下面的源码可以更深入的研究。

  AudioChatRoom.rar  

  最后,跟大家说说部署的步骤:

(1)将服务端部署在一台机器上,启动服务端。

(2)修改客户端配置文件中的ServerIP为刚才服务器的IP。

(3)在多台机器上运行客户端,以不同的帐号登录到同一个房间(如默认的R1000)。

(4)如此,多个用户就处于同一个聊天室进行语音聊天了。

  

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。