How can I play byte array of audio raw data using NAudio?

前端 未结 2 1103
长情又很酷
长情又很酷 2020-12-11 03:50
byte[] bytes = new byte[1024];

Assume bytes is an array filled with audio raw data.

How can I play this byte array using a

2条回答
  •  被撕碎了的回忆
    2020-12-11 04:26

    The accepted answer assumes the byte stream is 44,1kHz, 16 bit, stereo. If you have something else you have to provide the coding in the WaveFormat

    byte[] bytes = new byte[1024];
    
    IWaveProvider provider = new RawSourceWaveStream(
                             new MemoryStream(bytes), new WaveFormat(48000, 16, 1));
    
    _waveOut.Init(provider);
    _waveOut.Play();
    

    If your raw data is in fact a wav file you already have the encoding in the header and can use this method

    byte[] bytes = new byte[1024];
    
    WaveFileReader reader = new WaveFileReader(new MemoryStream(bytes));
    
    _waveOut.Init(reader);
    _waveOut.Play();
    

提交回复
热议问题