2017-10-21 92 views
1

我的c#代碼如下,它沒有提供任何錯誤,但沒有聲音輸出。我使用來自COM14端口的putty和受監控的數據流很好。這是我第一次在NAudio中,所以不知道那我做錯了什麼。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

有關於你的代碼

  • 什麼是波源對一些問題我 - 爲什麼是一個正在創建每次從COM端口接收數據的時間?
  • 您應該只在waveOut上調用InitPlay,但是您在接收到的每個音頻緩衝區上調用Play

我推薦的解決這類問題的方法是將收到的音頻寫入WAV文件WaveFileWriter。通過這種方式,您可以在常規波形編輯器中查看它是否包含您期望的信號。