NAudio流播放器没有声音输出

时间:2017-10-21 10:56:27

标签: c# naudio

我的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;
    }

1 个答案:

答案 0 :(得分:1)

我对您的代码有一些问题

  • 什么是waveSource - 为什么每次从COM端口接收数据时都会创建一个?
  • 您应该只在waveOut上拨打InitPlay一次,但是您在收到的每个音频缓冲区上都会调用Play

我建议的解决此类问题的方法是将收到的音频写入包含WaveFileWriter的WAV文件。这样,您可以在常规波形编辑器中检查它,看它是否包含您期望的信号。