C#基于UDP實(shí)現(xiàn)的P2P語(yǔ)音聊天工具
語(yǔ)音獲取
要想發(fā)送語(yǔ)音信息,首先得獲取語(yǔ)音,這里有幾種方法,一種是使用DirectX的DirectXsound來錄音,我為了簡(jiǎn)便使用一個(gè)開源的插件NAudio來實(shí)現(xiàn)語(yǔ)音錄取。 在項(xiàng)目中引用NAudio.dll
//------------------錄音相關(guān)----------------------------- private IWaveIn waveIn; private WaveFileWriter writer; private void LoadWasapiDevicesCombo() { var deviceEnum = new MMDeviceEnumerator(); var devices = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToList(); comboBox1.DataSource = devices; comboBox1.DisplayMember = "FriendlyName"; } private void CreateWaveInDevice() { waveIn = new WaveIn(); waveIn.WaveFormat = new WaveFormat(8000, 1); waveIn.DataAvailable += OnDataAvailable; waveIn.RecordingStopped += OnRecordingStopped; } void OnDataAvailable(object sender, WaveInEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new EventHandler<WaveInEventArgs>(OnDataAvailable), sender, e); } else { writer.Write(e.Buffer, 0, e.BytesRecorded); int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond); if (secondsRecorded >= 10)//最大10s { StopRecord(); } else { l_sound.Text = secondsRecorded + " s"; } } } void OnRecordingStopped(object sender, StoppedEventArgs e) { if (InvokeRequired) { BeginInvoke(new EventHandler<StoppedEventArgs>(OnRecordingStopped), sender, e); } else { FinalizeWaveFile(); } } void StopRecord() { AllChangeBtn(btn_luyin, true); AllChangeBtn(btn_stop, false); AllChangeBtn(btn_sendsound, true); AllChangeBtn(btn_play, true); //btn_luyin.Enabled = true; //btn_stop.Enabled = false; //btn_sendsound.Enabled = true; //btn_play.Enabled = true; if (waveIn != null) waveIn.StopRecording(); //Cleanup(); } private void Cleanup() { if (waveIn != null) { waveIn.Dispose(); waveIn = null; } FinalizeWaveFile(); } private void FinalizeWaveFile() { if (writer != null) { writer.Dispose(); writer = null; } } //開始錄音 private void btn_luyin_Click(object sender, EventArgs e) { btn_stop.Enabled = true; btn_luyin.Enabled = false; if (waveIn == null) { CreateWaveInDevice(); } if (File.Exists(soundfile)) { File.Delete(soundfile); } writer = new WaveFileWriter(soundfile, waveIn.WaveFormat); waveIn.StartRecording(); }
上面的代碼實(shí)現(xiàn)了錄音,并且寫入文件p2psound_A.wav
語(yǔ)音發(fā)送
獲取到語(yǔ)音后我們要把語(yǔ)音發(fā)送出去
當(dāng)我們錄好音后點(diǎn)擊發(fā)送,這部分相關(guān)代碼是
MsgTranslator tran = null; ublic Form1() { InitializeComponent(); LoadWasapiDevicesCombo();//顯示音頻設(shè)備 Config cfg = SeiClient.GetDefaultConfig(); cfg.Port = 7777; UDPThread udp = new UDPThread(cfg); tran = new MsgTranslator(udp, cfg); tran.MessageReceived += tran_MessageReceived; tran.Debuged += new EventHandler<DebugEventArgs>(tran_Debuged); } private void btn_sendsound_Click(object sender, EventArgs e) { if (t_ip.Text == "") { MessageBox.Show("請(qǐng)輸入ip"); return; } if (t_port.Text == "") { MessageBox.Show("請(qǐng)輸入端口號(hào)"); return; } string ip = t_ip.Text; int port = int.Parse(t_port.Text); string nick = t_nick.Text; string msg = "語(yǔ)音消息"; IPEndPoint remote = new IPEndPoint(IPAddress.Parse(ip), port); Msg m = new Msg(remote, "zz", nick, Commands.SendMsg, msg, "Come From A"); m.IsRequireReceive = true; m.ExtendMessageBytes = FileContent(soundfile); m.PackageNo = Msg.GetRandomNumber(); m.Type = Consts.MESSAGE_BINARY; tran.Send(m); } private byte[] FileContent(string fileName) { FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); try { byte[] buffur = new byte[fs.Length]; fs.Read(buffur, 0, (int)fs.Length); return buffur; } catch (Exception ex) { return null; } finally { if (fs != null) { //關(guān)閉資源 fs.Close(); } } }
如此一來我們就把產(chǎn)生的語(yǔ)音文件發(fā)送出去了
語(yǔ)音的接收與播放
其實(shí)語(yǔ)音的接收和文本消息的接收沒有什么不同,只不過語(yǔ)音發(fā)送的時(shí)候是以二進(jìn)制發(fā)送的,因此我們?cè)谑盏秸Z(yǔ)音后 就應(yīng)該寫入到一個(gè)文件里面去,接收完成后,播放這段語(yǔ)音就行了。
下面這段代碼主要是把收到的數(shù)據(jù)保存到文件中去,這個(gè)函數(shù)式我的NetFrame里收到消息時(shí)所觸發(fā)的事件,在文章前面提過的那篇文章里
void tran_MessageReceived(object sender, MessageEventArgs e) { Msg msg = e.msg; if (msg.Type == Consts.MESSAGE_BINARY) { string m = msg.Type + "->" + msg.UserName + "發(fā)來二進(jìn)制消息!"; AddServerMessage(m); if (File.Exists(recive_soundfile)) { File.Delete(recive_soundfile); } FileStream fs = new FileStream(recive_soundfile, FileMode.Create, FileAccess.Write); fs.Write(msg.ExtendMessageBytes, 0, msg.ExtendMessageBytes.Length); fs.Close(); //play_sound(recive_soundfile); ChangeBtn(true); } else { string m = msg.Type + "->" + msg.UserName + "說:" + msg.NormalMsg; AddServerMessage(m); } }
收到語(yǔ)音消息后,我們要進(jìn)行播放,播放時(shí)仍然用剛才那個(gè)插件播放
//--------播放部分---------- private IWavePlayer wavePlayer; private WaveStream reader; public void play_sound(string filename) { if (wavePlayer != null) { wavePlayer.Dispose(); wavePlayer = null; } if (reader != null) { reader.Dispose(); } reader = new MediaFoundationReader(filename, new MediaFoundationReader.MediaFoundationReaderSettings() { SingleReaderObject = true }); if (wavePlayer == null) { wavePlayer = new WaveOut(); wavePlayer.PlaybackStopped += WavePlayerOnPlaybackStopped; wavePlayer.Init(reader); } wavePlayer.Play(); } private void WavePlayerOnPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs) { if (stoppedEventArgs.Exception != null) { MessageBox.Show(stoppedEventArgs.Exception.Message); } if (wavePlayer != null) { wavePlayer.Stop(); } btn_luyin.Enabled = true; }private void btn_play_Click(object sender, EventArgs e) { btn_luyin.Enabled = false; play_sound(soundfile); }
在上面演示了接收和發(fā)送一段語(yǔ)音消息的界面
技術(shù)總結(jié)
主要用到的技術(shù)就是UDP和NAudio的錄音和播放功能
希望這篇文章能夠給大家提供一個(gè)思路,幫助大家實(shí)現(xiàn)P2P語(yǔ)音聊天工具。
相關(guān)文章
C# winform 模擬鍵盤輸入自動(dòng)接入訪問網(wǎng)絡(luò)的實(shí)例
本篇文章主要介紹了C# winform 模擬鍵盤輸入自動(dòng)接入訪問網(wǎng)絡(luò),有興趣的可以了解一下。2016-11-11Unity UGUI實(shí)現(xiàn)簡(jiǎn)單拖拽圖片功能
這篇文章主要為大家詳細(xì)介紹了Unity UGUI實(shí)現(xiàn)簡(jiǎn)單拖拽圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06AOP從靜態(tài)代理到動(dòng)態(tài)代理(Emit實(shí)現(xiàn))詳解
AOP為Aspect Oriented Programming的縮寫,意思是面向切面編程的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于AOP從靜態(tài)代理到動(dòng)態(tài)代理(Emit實(shí)現(xiàn))的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-09-09C#在DataTable中根據(jù)條件刪除某一行的實(shí)現(xiàn)方法
我們通常的方法是把數(shù)據(jù)源放在DataTable里面,但是偶爾也會(huì)需要把不要的行移除,怎么實(shí)現(xiàn)呢,下面通過代碼給大家介紹c# atatable 刪除行的方法,需要的朋友一起看下吧2016-05-05Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)繞任意軸任意角度旋轉(zhuǎn)向量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01C#中的lock、Monitor、Mutex學(xué)習(xí)筆記
這篇文章主要介紹了C#中的lock、Monitor、Mutex學(xué)習(xí)筆記,本文講解的都是線程同步的一些知識(shí),需要的朋友可以參考下2015-01-01C#用正則表達(dá)式Regex.Matches 方法檢查字符串中重復(fù)出現(xiàn)的詞
使用正則表達(dá)式用Regex類的Matches方法,可以檢查字符串中重復(fù)出現(xiàn)的詞,Regex.Matches方法在輸入字符串中搜索正則表達(dá)式的所有匹配項(xiàng)并返回所有匹配,本文給大家分享C#正則表達(dá)式檢查重復(fù)出現(xiàn)的詞,感興趣的朋友一起看看吧2024-02-02