Search code examples
c#naudiowave

How to convert a byte array into WaveStream in NAudio?


I want to convert a byte array (read from mp3 file) into a WaveStream, then create a WaveChannel32 to play audio in NAudio. I can read byte array to Stream, then to Mp3FileReader but it does not allow me to change volume. So I have to use WaveChannel32 instead.


Solution

  • You can pass the Mp3FileReader to the WaveChannel32 which will allow you to pan and change volume.

    var mp3Bytes = File.ReadAllBytes("d:/Music/RICHARD JOSEPH - Gods17.mp3");
    using (var mp3Stream = new MemoryStream(mp3Bytes))
    {
        using (var mp3FileReader = new Mp3FileReader(mp3Stream))
        {
            using (var wave32 = new WaveChannel32(mp3FileReader, 0.1f, 1f))
            {
                using (var ds = new DirectSoundOut())
                {
                    ds.Init(wave32);
                    ds.Play();
                    Thread.Sleep(30000);
                }
            }
        }
    }