无法理解问题所在。
有一台服务器正在接受来自客户端的数据并发回。客户端发送从麦克风录制的声音,当从服务器接收时播放该声音。 出于某种原因,如果缓冲区超过47毫秒,客户端不希望从服务器接收数据。但是,即使缓冲区保持45毫秒的声音,也不会播放从服务器接收的声音。如果你创建一个缓冲区,例如30毫秒,那么从服务器接收的音频将是可播放的,但它将是错误的(你只能听到敲门声)。 一些超自然现象?
using NAudio.Wave;
using System.Net.Sockets;
class Program
{
private void Main()
{
Recorder rec = new Recorder();
Console.ReadKey();
}
}
class Recorder
{
UdpClient client = new UdpClient(755);
WaveInEvent wave = new WaveInEvent();
public Recorder()
{
Thread play = new Thread(new ThreadStart(Play));
play.Start();
wave.BufferMilliseconds = 100;
wave.DeviceNumber = 0;
wave.WaveFormat = new WaveFormat(8000, 16, 2);
wave.DataAvailable += Wave_DataAvailable;
wave.StartRecording();
}
private void Play()
{
WaveOutEvent output = new WaveOutEvent();
BufferedWaveProvider buffer = new BufferedWaveProvider(new WaveFormat(8000, 16, 2));
output.Init(buffer);
output.Play();
for(;;)
{
IPEndPoint remoteEP = null;
byte[] data = client.Receive(ref remoteEP);
buffer.AddSamples(data, 0, data.Length);
}
}
private void Wave_DataAvailable(object sender, WaveInEventArgs e)
{
client.Send(e.Buffer, e.BytesRecorded, "46.174.49.51", 54321);
}
}
为了说服您服务器确实将数据发送回客户端,这里是代码:
UdpClient client = new UdpClient(755);
byte[] sdata = Encoding.UTF8.GetBytes("Test");
client.Send(sdata, sdata.Length, "46.174.49.51", 54321);
IPEndPoint remoteEP = null;
byte[] rdata = client.Receive(ref remoteEP);
Console.WriteLine(Encoding.UTF8.GetString(rdata) + " from " + remoteEP.Port);
Console.ReadLine();