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

懵懂的女人 提交于 2019-11-29 13:51:42

I figured it out, here is the solution:

byte[] bytes = new byte[1024];

IWaveProvider provider = new RawSourceWaveStream(
                         new MemoryStream(bytes), new WaveFormat());

_waveOut.Init(provider);
_waveOut.Play();

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();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!