我的c#代码如下,它没有错误,但也没有声音输出。我使用来自COM14端口的putty和监控数据流很好。这是我第一次来到NAudio所以不知道我做错了什么
WaveInEvent waveSource;
WaveOut waveOut = new WaveOut();
WaveFormat format = new WaveFormat(8000, 16, 1);
public int audioCounter = 0;
public byte[] buffer;
private BufferedWaveProvider _bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 1));
private void btn_scan_Click(object sender, EventArgs e)
{
string temppoteName = "COM14";
SerialPort tempSerialPort = new SerialPort(temppoteName, 57600, Parity.None, 8, StopBits.One);
tempSerialPort.Open();
tempSerialPort.DataReceived += new SerialDataReceivedEventHandler(comPort14_DataReceived);
}
private void comPort14_DataReceived(object sender,SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Started Reciving :" + sp.PortName);
waveSource = new WaveInEvent();
waveSource.WaveFormat = new WaveFormat(8000, 16, 1);
waveOut.DesiredLatency = 100;
int x = sp.BytesToRead;
buffer = new byte[x];
sp.Read(buffer, 0, x);
_bufferedWaveProvider.AddSamples(buffer, 0, x);
if (audioCounter == 0)
{
waveOut.Init(_bufferedWaveProvider);
}
waveOut.Play();
++audioCounter;
}
答案 0 :(得分:1)
我对您的代码有一些问题
Init
和Play
一次,但是您在收到的每个音频缓冲区上都会调用Play
。我建议的解决此类问题的方法是将收到的音频写入包含WaveFileWriter
的WAV文件。这样,您可以在常规波形编辑器中检查它,看它是否包含您期望的信号。