private void SendAudioMessage() { if (File.Exists(audiofilename) && isRecord == 1) { //Sending to server byte[] bytes = Encoding.UTF8.GetBytes("audiofile"); stream.Write(bytes, 0, bytes.Length); SendAudio audio = new SendAudio(); audio.SetFile(audiofilename); audio.AudioTime = GetWavFileDuration(audiofilename).ToString(); using (FileStream fstream = new FileStream(audiofilename, FileMode.Open, FileAccess.Read)) { SendFileInfo(fstream); SendFile(fstream); } //Ending send file //Locate control on GUI audio.Left = this.Width - 230; CheckScrollBar(); audio.Top = getPosition(); audio.AddTimeLabelSender(); panel3.Controls.Add(audio); lastObject = audio; audiolist.Add(audio); //Ending add control } }
void UpdateSoundSamples(SendAudio sound) { // Don't do anything if this is on the current player (which we have write access to) if (AudioTransmissionReader.HasAuthority) { return; } Debug.Log("UpdateSoundSamples"); // If the clip isn't playing, it needs initialising if (!audioSource.isPlaying) { InitialiseAudioSource(); } // Add the new audio data at the set offset Debug.LogFormat("sound.samples: {0}, lastSamplePlayed: {1}", sound.samples.Count, lastSamplePlayed); audioSource.clip.SetData(sound.samples.ToArray(), lastSamplePlayed); // If the clip isn't already playing, set it off if (!audioSource.isPlaying) { audioSource.PlayDelayed(0.1f); } // If audio clip buffer overflowed, we wrap around from the start lastSamplePlayed = (lastSamplePlayed + sound.samples.Count) % SimulationSettings.MaxAudioClipSamples; }
private void ModCommunicationAudio() { while (mainthread.ThreadState != System.Threading.ThreadState.Stopped) { try { if (Audio == null) { Audio = new AnonymousPipeClientStream(PipeDirection.In, CaptureStreamForm.AudioStream.GetClientHandleAsString()); if (Video != null) { CaptureStreamForm.isConnected = true; } continue; } if (Audio.CanRead) { int headersize = sizeof(int) * 4; int read = 0; do { read += Audio.Read(transferabuffer, read, headersize - read); }while (read < headersize); int bytes = BitConverter.ToInt32(transferabuffer, sizeof(int)); //int samplerate = BitConverter.ToInt32(transferabuffer, sizeof(int)); if (bytes + headersize > transferabuffer.Length) { var oldbuf = transferabuffer; transferabuffer = new byte[bytes + headersize]; //grow automatically. Buffer.BlockCopy(oldbuf, 0, transferabuffer, 0, headersize); //move size header to new array. } int rest = 0; do { rest += Audio.Read(transferabuffer, rest + headersize, bytes - rest); }while (rest < bytes); SendAudio?.Invoke(transferabuffer, bytes + headersize); } } catch (Exception ex) { MyLog.Default.WriteLineAndConsole(ex.ToString()); } if (process.HasExited) { break; } Thread.Sleep(0); } }
public void PlayAudio(MemoryStream ms) { try { buffDes = new BufferDescription(SendAudio.SetWaveFormat()); buffDes.GlobalFocus = true; //设置缓冲区全局获取焦点 buffDes.ControlVolume = true; //指明缓冲区可以控制声音 buffDes.ControlPan = true; //指明缓冲区可以控制声道平衡 buffDes.BufferBytes = (int)ms.Length; MemoryStream newms = new MemoryStream(ms.ToArray()); SecondaryBuffer sec = new SecondaryBuffer(newms, buffDes, device); //newms.Seek(0, SeekOrigin.Begin); //sec.Write(0,newms, (int)ms.Length, LockFlag.FromWriteCursor); sec.Play(0, BufferPlayFlags.Default); } catch { } }
private int getPosition() { if (msglist.Count == 0 && receivelist.Count == 0 && photolist.Count == 0 && filelist.Count == 0 && audiolist.Count == 0 && videolist.Count == 0) { return(40); } if (lastObject is MyBubble) { MyBubble tmp = (MyBubble)lastObject; return(tmp.Top + tmp.Height); } if (lastObject is GetBubble) { GetBubble tmp = (GetBubble)lastObject; return(tmp.Top + tmp.Height); } if (lastObject is SendImage) { SendImage tmp = (SendImage)lastObject; return(tmp.Top + tmp.Height); } if (lastObject is SendFile) { SendFile tmp = (SendFile)lastObject; return(tmp.Top + tmp.Height); } if (lastObject is SendAudio) { SendAudio tmp = (SendAudio)lastObject; return(tmp.Top + tmp.Height); } if (lastObject is SendVideo) { SendVideo tmp = (SendVideo)lastObject; return(tmp.Top + tmp.Height); } else { return(0); } }
private void timer1_Tick_1(object sender, EventArgs e) { if (gettedMessage != "") { ReceiveBubble(gettedMessage); gettedMessage = ""; } if (path != "") { image = new SendImage(); image.SetImage(path); image.Left = 5; image.PhotoColor = Color.SkyBlue; image.Top = getPosition(); image.AddTimeLabelGetter(); panel3.Controls.Add(image); lastObject = image; photolist.Add(image); path = ""; CheckScrollBar(); } if (filepath != "") { file = new SendFile(); file.SetFile(filepath); file.FileName = filepath; file.FileSize = fileSizeString; file.Left = 5; file.Top = getPosition(); file.AddTimeLabelGetter(); panel3.Controls.Add(file); lastObject = file; filelist.Add(file); filepath = ""; CheckScrollBar(); } if (audiopath != "") { audio = new SendAudio(); audio.SetFile(audiopath); audio.AudioTime = (GetWavFileDuration(audiopath)).ToString(); audio.Left = 5; audio.Top = getPosition(); audio.AddTimeLabelGetter(); panel3.Controls.Add(audio); lastObject = audio; audiolist.Add(audio); audiopath = ""; CheckScrollBar(); } if (videopath != "") { wideo = new SendVideo(); wideo.SetFile(videopath); wideo.Left = 5; wideo.Top = getPosition(); wideo.AddTimeLabelGetter(); panel3.Controls.Add(wideo); lastObject = wideo; videolist.Add(wideo); videopath = ""; CheckScrollBar(); } }
private void ReceiveCallback(IAsyncResult ar) { StateObject state = ar.AsyncState as StateObject; UdpClient udpClient = state.workSocket; IPEndPoint remote = null; Byte[] data; try{ data = udpClient.EndReceive(ar, ref remote); } catch { return; } //查看是什么类型 byte[] bnum = new byte[4]; for (int i = 0; i < 4; i++) { bnum[i] = data[i]; } int num = BitConverter.ToInt32(bnum, 0); switch (num) { case 10: state.mstext.Write(data, 4, data.Length - 4); break; case 11: state.mstext.Write(data, 4, data.Length - 4); this.Dispatcher.BeginInvoke(new DelegateWriteTextBox(WriteTextBox), SendText.Reveive(state.mstext)); state.mstext = new MemoryStream(); break; case 20: state.ms.Write(data, 4, data.Length - 4); break; case 21: state.ms.Write(data, 4, data.Length - 4); this.Dispatcher.BeginInvoke(new DelegateWriteImage(WriteImage), SendVideo.Reveive(state.ms)); state.ms = new MemoryStream(); break; case 30: state.msvoice.Write(data, 4, data.Length - 4); break; case 31: state.msvoice.Write(data, 4, data.Length - 4); this.Dispatcher.BeginInvoke(new DelegatePlayAudio(PlayAudio), SendAudio.Receive(state.msvoice)); state.msvoice = new MemoryStream(); break; default: break; } try { state.buffer = new byte[StateObject.lenght]; udpReceive.BeginReceive(new AsyncCallback(ReceiveCallback), state); } catch { throw; } }
/// 语音 private void Button_Click_1(object sender, RoutedEventArgs e) { SendAudio sa = new SendAudio(remoteEP, 31, 30); sa.Start(); }
//エントリポイント static void Main(string[] args) { UPnPWanService napt = null; //UPnPサービス //UPnPを使用するか確認 Console.Write("UPnPを使用してNAT外の端末と通信を行いますか? [Y(既定)/N] > "); if (Console.ReadLine().ToUpper() != "N") { //UPnPを使用 bool naptSearchComplete = false; var task = new Task(() => { Console.Write("UPnP対応ルータを探索中"); while (naptSearchComplete == false) { Console.Write("."); Thread.Sleep(1000); } Console.WriteLine(""); }); task.Start(); //UPnP対応ルータを探索 napt = UPnPWanService.FindUPnPWanService(); naptSearchComplete = true; task.Wait(); if (napt != null) { //ポートマッピングを追加 try { napt.AddPortMapping(null, 50003, "UDP", 50003, napt.GetLocalIPAddress(), true, "AudioExchangerCs", 3600); Console.WriteLine("ポートマッピングを追加しました"); } catch (System.Runtime.InteropServices.COMException exception) { Console.WriteLine(exception.Message); napt = null; } } else { Console.WriteLine("UPnP対応ルータが見つかりませんでした"); } } //通話待受IPアドレスを表示 if (napt != null) { Console.WriteLine($"{napt.GetLocalIPAddress().ToString()} もしくは {napt.GetExternalIPAddress().ToString()} で通話を受け付けます\r\n"); } else { IPAddress[] localAddresses = Dns.GetHostAddresses(Dns.GetHostName()); foreach (IPAddress address in localAddresses) { if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { Console.Write($"{address.ToString()}, "); } } Console.CursorLeft = Console.CursorLeft - 2; Console.WriteLine($" で通話を受け付けます\r\n"); } //通話待受処理ループ while (true) { var receiveAudio = new ReceiveAudio(); var sendAudio = new SendAudio(); var source = new CancellationTokenSource(); IPAddress remoteAddress; //接続先のIPアドレスを入力 Console.Write("接続先IPアドレス・ホスト名 (exit:終了) > "); string remoteHost = Console.ReadLine(); //終了コマンド if (remoteHost == "exit") { break; } //String型のIPアドレスとしてIPAddress型にParse else if (IPAddress.TryParse(remoteHost, out remoteAddress) == false) { try { //String型のDNS名としてIPAddress型に名前解決 remoteAddress = Dns.GetHostEntry(remoteHost).AddressList[0]; } catch (Exception) { Console.WriteLine("正しくないIPアドレスもしくはホスト名が入力されました"); continue; } } Console.WriteLine($"Enterキーを押すと{remoteAddress.ToString()}と通話を開始します..."); Console.ReadLine(); //タスクを生成 var task_r = new Task(async() => await receiveAudio.StartReceiveAsync(source)); var task_s = new Task(async() => await sendAudio.StartSendAsync(remoteAddress, source)); //タスクを開始 task_r.Start(); task_s.Start(); Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine($"[通話開始] ({DateTime.Now.ToString("HH:mm:ss")}) -->{remoteAddress.ToString()}({sendAudio.RemotePort})"); Console.WriteLine("Enterキーを押すと通話を終了します..."); Console.ReadLine(); //タスクのキャンセル要求を発行(通話終了) source.Cancel(); try { //タスクが終了するまで待機 task_r.Wait(); task_s.Wait(); } catch (AggregateException exception) { Console.WriteLine(exception.Message); } finally { Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine($"[通話終了] ({DateTime.Now.ToString("HH:mm:ss")})\r\n"); } } //UPnPが有効な場合ポートマッピングを削除 if (napt != null) { try { napt.DeletePortMapping(null, 50003, "UDP"); Console.WriteLine("ポートマッピング削除完了"); } catch (System.Runtime.InteropServices.COMException exception) { Console.WriteLine(exception.Message); } } }
public static ValidationResult <SendAudio> CreateValidation(this SendAudio value) => new ValidationResult <SendAudio>(value) .ValidateRequired(x => x.ChatId) .ValidateRequired(x => x.Audio);