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

2

10
byte[] bytes = new byte[1024];

Assume bytes is an array filled with audio raw data.

How can I play this byte array using a WaveOut object?

_waveOut.Init(bytes); // <- Error: cannot resolve method.
_waveOut.Play();
Alvie answered 1/3, 2015 at 10:35 Comment(0)
A
16

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();
Alvie answered 1/3, 2015 at 16:31 Comment(1)
when i play the audio pitch is ihgher than normalRenz
S
12

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();
Selfinterest answered 24/2, 2017 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.