2

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.

Alex Huynh
  • 384
  • 3
  • 11
  • There is a similar post- http://stackoverflow.com/questions/184683/play-audio-from-a-stream-using-c-sharp not sure if it can solve your problem. – Souvik Ghosh May 31 '16 at 09:17

3 Answers3

2

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);
            }
        }
    }
}
devlead
  • 4,935
  • 15
  • 36
2

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

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
  • I have a question for you. how can i create an mp3 file from bytes array? i have converted a file into bytes using ReadAllBytes(file); in c#. please help. – Salman Lone Oct 10 '17 at 13:36
  • and please let me know if you can help me in changing the channels mono/stereo while converting file type from .wav to .mp3.... i am using LAME. – Salman Lone Oct 10 '17 at 13:37
1
 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();
A D
  • 51
  • 2
  • 12