|
|
|
ส่งเสียงพูด input Mic to Output Speaker ส่งได้ไม่นาน ช่วยผมด้วยครับ แนะนำหน่อยครับ |
|
|
|
|
|
|
|
SERVER (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Media;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;
namespace ReceiverSound
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem(startListening);
}
Socket hostSocket, receiveSocket, sock; Thread thread;
private void startListening(object state)
{
try
{
receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint hostIpEndPoint = new IPEndPoint(IPAddress.Any, 9050);
//Connection node
receiveSocket.Bind(hostIpEndPoint);
receiveSocket.Listen(10);
MessageBox.Show("Start");
hostSocket = receiveSocket.Accept();
rec_thread = new Thread(new ThreadStart(VoiceReceive));
rec_thread.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
private NetworkStream ns;
private Thread rec_thread;
private void WriteBytes()
{
if (ns != null)
{
SoundPlayer sp = new SoundPlayer(ns);
sp.Play();
}
}
private void VoiceReceive()
{
sock = receiveSocket.Accept();
ns = new NetworkStream(sock);
WriteBytes();
receiveSocket.Close();
while (true)
{
VoiceReceive();
}
}
public static TimeSpan GetSoundLength(string fileName)
{
WaveFileReader wf = new WaveFileReader(fileName);
return wf.TotalTime;
}
}
}
CLIENT (C#)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Media;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio.Wave;
namespace SendSound
{
class voice
{
private string ip;
private string path = Application.StartupPath + "\\buffer.wav";
public string Ip
{
get { return ip; }
set { ip = value; }
}
private int port;
public int VPort
{
get { return port; }
private set { port = value; }
}
private DirectSoundOut waveOut = null;
private WaveIn sourceStream = null;
private Byte[] Data_ary;
private WaveFileWriter waveWriter = null;
private System.Windows.Forms.Timer c_v = null;
private Socket connector;
public void Send(string ip, int port)
{
this.Ip=ip;
this.VPort = port;
//timemer
c_v = new System.Windows.Forms.Timer();
c_v.Interval = 2000;
c_v.Enabled = false;
c_v.Start();
//send
c_v.Tick += c_v_Tick;
Recordwav();
}
private void Recordwav()
{
sourceStream = new WaveIn();
int devicenum = 0;
// SaveFileDialog save = new SaveFileDialog();
// save.Filter = "Wave File (*.wav)|*.wav;";
for (int i = 0; i < NAudio.Wave.WaveIn.DeviceCount; i++)
{
if (NAudio.Wave.WaveIn.GetCapabilities(i).ProductName.Contains("icrophone"))
devicenum = i;
}
sourceStream = new NAudio.Wave.WaveIn();
sourceStream.DeviceNumber = devicenum;
sourceStream.WaveFormat = new WaveFormat(44100, WaveIn.GetCapabilities(devicenum).Channels);
sourceStream.DataAvailable += new EventHandler<WaveInEventArgs>(sourceStream_DataAvailable);
WaveInProvider waveIn = new NAudio.Wave.WaveInProvider(sourceStream);
waveWriter = new WaveFileWriter(path, sourceStream.WaveFormat);
//waveOut = new NAudio.Wave.DirectSoundOut();
// waveOut.Init(waveIn);
sourceStream.StartRecording();
c_v.Start();
}
void c_v_Tick(object sender, EventArgs e)
{
this.Dispose();
Send_Bytes();
}
private void Send_Bytes()
{
Data_ary = File.ReadAllBytes(path);
connector = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ie = new IPEndPoint(IPAddress.Parse(this.Ip), this.VPort);
ie.Address = IPAddress.Loopback;
connector.Connect(ie);
connector.Send(Data_ary, 0, Data_ary.Length, 0);
connector.Close();
Recordwav();
}
private void sourceStream_DataAvailable(object sender, WaveInEventArgs e)
{
if (waveWriter == null) return;
waveWriter.WriteData(e.Buffer, 0, e.BytesRecorded);
waveWriter.Flush();
}
//not used here but its useful to get the length of wav file
public static TimeSpan GetSoundLength(string fileName)
{
WaveFileReader wf = new WaveFileReader(fileName);
return wf.TotalTime;
}
private void Dispose()
{
c_v.Stop();
if (sourceStream != null)
{
sourceStream.StopRecording();
sourceStream.Dispose();
}
if (waveWriter != null)
{
waveWriter.Dispose();
}
GC.SuppressFinalize(this);
}
}
}
มันหยุด reccord ไปเสียก่อน ผมว่าน่าจะเป็นที่ timer ข้อความกรุณา แนะนำหน่อยครับ อยากให้ส่งเสียง ได้ไม่หลุด
Tag : .NET, Win (Windows App), C#
|
|
|
|
|
|
Date :
2016-02-29 23:45:15 |
By :
zeuszerome |
View :
978 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองปรับดูแล้วครับ c_v.Interval = 2000;
2000 คือ สองวินาที ผลที่ได้เหมือนเดิมครับ คือ เหมือนมันส่งข้อมุลไม่ทัน record
มีอะไรที่ใช้แทน timer ได้ป่าวครับ
|
|
|
|
|
Date :
2016-03-02 18:57:09 |
By :
zeuszerome |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองปรับมากกว่านี้ได้หรือเล่าครับ
|
|
|
|
|
Date :
2016-03-03 09:03:20 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|