找传奇、传世资源到传世资源站!

c#语音对讲demo

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

开启前输入对方IP点击录音然后发送即可from clipboardusing NAudio.CoreAudioApi;using NAudio.Wave;using Netframe.Core;using Netframe.Event;using Netframe;using Netframe.Model;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Net;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Windows.Input;using System.Threading;namespace 语音{ public partial class Form1 : Form { // MsgTranslator tran = null; public Form1() { InitializeComponent(); LoadWasapiDevicesCombo(); CheckForIllegalCrossThreadCalls = false; //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 IWaveIn waveIn; private WaveFileWriter writer; private IWavePlayer wavePlayer; private WaveStream reader; //麦克风 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 >= 30)//最大30s { StopRecord(); } else { l_sound.Text = secondsRecorded 1 " s"; } } } void OnRecordingStopped(object sender, StoppedEventArgs e) { if (InvokeRequired) { BeginInvoke(new EventHandler<StoppedEventArgs>(OnRecordingStopped), sender, e); } else { FinalizeWaveFile(); } } void StopRecord() { if (waveIn != null) waveIn.StopRecording(); } private void Cleanup() { if (waveIn != null) { waveIn.Dispose(); waveIn = null; } FinalizeWaveFile(); } private void FinalizeWaveFile() { if (writer != null) { writer.Dispose(); writer = null; } } string soundfile; private void button1_Click(object sender, EventArgs e) { soundfile = DateTime.Now.Minute.ToString () DateTime .Now.Second.ToString() ".wav"; if (wavePlayer != null) { wavePlayer.Dispose(); wavePlayer = null; } if (waveIn == null) { CreateWaveInDevice(); } if (File.Exists(soundfile)) { File.Delete(soundfile); } writer = new WaveFileWriter(soundfile, waveIn.WaveFormat); waveIn.StartRecording(); int i = (int)MessageBox.Show("开始录制:点击确定结束录制;点击取消取消录制.", "系统提示:", MessageBoxButtons.OKCancel); if (i == 1) { StopRecord(); l_sound.Text = 0 " s"; } else if(i == 2) { StopRecord(); l_sound.Text = 0 " s"; } } //---------------------------------------播放相关----------------- public void play_sound(string filename) { if (wavePlayer != null) { wavePlayer.Dispose(); wavePlayer = null; } if (reader != null) { reader.Dispose(); } try { reader = new MediaFoundationReader(filename, new MediaFoundationReader.MediaFoundationReaderSettings() { SingleReaderObject = true }); if (wavePlayer == null) { wavePlayer = new WaveOut(); wavePlayer.PlaybackStopped = WavePlayerOnPlaybackStopped; wavePlayer.Init(reader); } wavePlayer.Play(); } catch { MessageBox.Show("当前录音为空"); } } private void WavePlayerOnPlaybackStopped(object sender, StoppedEventArgs stoppedEventArgs) { if (stoppedEventArgs.Exception != null) { MessageBox.Show(stoppedEventArgs.Exception.Message); } if (wavePlayer != null) { wavePlayer.Stop(); } } private void btn_play_Click(object sender, EventArgs e) { play_sound(soundfile); } //-----------------------发送相关----------------- 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) { //关闭资源 fs.Close(); } } } private delegate void MessageDelegate(string message); public void AddServerMessage(string message) { if (richTextBox1.InvokeRequired)//不能访问就创建委托 { MessageDelegate d = new MessageDelegate(AddServerMessage); richTextBox1.Invoke(d, new object[] { message }); } else { richTextBox1.AppendText(message Environment.NewLine); richTextBox1.ScrollToCaret(); } } string recive_soundfile; void tran_MessageReceived(object sender, MessageEventArgs e) { recive_soundfile = DateTime.Now.Minute.ToString() DateTime.Now.Second.ToString() ".wav"; Msg msg = e.msg; if (msg.Type == Consts.MESSAGE_BINARY) { string m = msg.Type "->" msg.UserName "发来一条语音消息"; 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); } } private void button3_Click_1(object sender, EventArgs e) { if(soundfile!=null ) { if (t_ip.Text == "") { MessageBox.Show("请输入ip"); return; } string ip = t_ip.Text; UDPThread udp = new UDPThread(cfg); MsgTranslator tran = new MsgTranslator(udp, cfg); tran.MessageReceived = tran_MessageReceived; IPEndPoint remote = new IPEndPoint(IPAddress.Parse(ip), 2048); Msg m = new Msg(remote, "猫九先森", "Name", Commands.Entry, "chat", "chat"); m.IsRequireReceive = true; m.ExtendMessageBytes = FileContent(soundfile); m.PackageNo = Msg.GetRandomNumber(); m.Type = Consts.MESSAGE_BINARY; tran.Send(m); } else { MessageBox.Show("请录入一条语音"); } } Config cfg = SeiClient.GetDefaultConfig(); private void Form1_Load(object sender, EventArgs e) { cfg.Port = 2048; } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复