2
votes

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.

3
There is a similar post- stackoverflow.com/questions/184683/… not sure if it can solve your problem. - Souvik Ghosh

3 Answers

2
votes

I'd recommend just using the AudioFileReader class as that provides volume for you and uses an Mp3FileReader under the hood for MP3s.

1
votes

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);
            }
        }
    }
}
1
votes
 var ms = new MemoryStream(soundArray.ToArray());
            IWaveProvider provider = new RawSourceWaveStream(ms, new WaveFormat());
            var waveOut = new NAudio.Wave.WaveOut();
            waveOut.DeviceNumber = GetDeviceNumber();
            waveOut.Init(provider);
            waveOut.Play();