Play MP3 files

Dec 29, 2010 at 11:49 AM
Edited Dec 29, 2010 at 1:46 PM

I managed to play wave files, but is it also possible to play mp3 files?

Dec 29, 2010 at 5:50 PM

Did you read the documentation? (The tab left of Discussions)

http://naudio.codeplex.com/documentation

MP3 playing is described on the first page..:

http://naudio.codeplex.com/wikipage?title=MP3

 

Yuval

Dec 29, 2010 at 9:54 PM

Thanks I didnt saw the documentation. However now I followed the documentation and have one question left:

 

The Class MeteringSystem isn't recognized:

 

The type or namespace name 'MeteringStream' could not be found (are you missing a using directive or an assembly reference?) 

 

What Am I missing?

Dec 29, 2010 at 10:35 PM

I would Ignore MeteringStream - I couldn't find it either in NAudio.

This is what I use:
       private WaveStream m_blockAlignedStream = null;
       private WaveStream m_waveReader = null;
       private WaveChannel32 m_waveChannel = null;
        /// <summary>
        /// Creates an input WaveChannel
        /// </summary>
        /// <param name="filename"></param>
        private void CreateInputWaveChannel(string filename)
        {
            string fileExt = Path.GetExtension( filename.ToLower() );
            if ( fileExt == MP3Extension )
            {
                m_waveReader = new Mp3FileReader(filename);
                m_blockAlignedStream = new BlockAlignReductionStream(m_waveReader);
                // Wave channel - reads from file and returns raw wave blocks
                m_waveChannel = new WaveChannel32(m_blockAlignedStream);
            }
            else if ( fileExt == WAVExtension )
            {
                m_waveReader = new WaveFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
                {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                }
                if (m_waveReader.WaveFormat.BitsPerSample != 16)
                {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                       16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }
                
                m_waveChannel = new WaveChannel32(m_waveReader);
            }
            else if (fileExt == WMAExtension)
            {
                m_waveReader = new WMAFileReader(filename);
                if (m_waveReader.WaveFormat.Encoding != WaveFormatEncoding.Pcm)
                {
                    m_waveReader = WaveFormatConversionStream.CreatePcmStream(m_waveReader);
                    m_waveReader = new BlockAlignReductionStream(m_waveReader);
                }
                if (m_waveReader.WaveFormat.BitsPerSample != 16)
                {
                    var format = new WaveFormat(m_waveReader.WaveFormat.SampleRate,
                       16, m_waveReader.WaveFormat.Channels);
                    m_waveReader = new WaveFormatConversionStream(format, m_waveReader);
                }

                m_waveChannel = new WaveChannel32(m_waveReader);
            }
            else
            {
                throw new ApplicationException("Cannot create Input WaveChannel - Unknown file type: " + fileExt);
            }
        }

Yuval

Editor
Dec 29, 2010 at 11:40 PM
Edited Dec 30, 2010 at 11:02 AM

Sorry, my fault. I'll fix up the documentation when I get home. The call back isn't defined in that code because it's not being used in that example. Just needs to return the input stream.

I'll fix up that documentation this afternoon.

Good to know it's being refereed to though :-)

Sebastian

Editor
Dec 30, 2010 at 11:02 AM

The documentation has been updated to remove that issue.

Thanks for the feedback.


Sebastian

Dec 30, 2010 at 6:35 PM
Edited Dec 30, 2010 at 9:12 PM

Thanks a lot, now it works. However I had to changed it again, because only PCM is supported:

 

 

 private WaveStream CreateInputStream(string fileName)
        {
            WaveChannel32 inputStream;
            if (fileName.EndsWith(".mp3"))
            {
                WaveStream mp3Reader = new Mp3FileReader(fileName);
                var pStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(mp3Reader);
                var blockStream = new NAudio.Wave.BlockAlignReductionStream(pStream);
                inputStream = new WaveChannel32(blockStream);
            }
            else
            {
                throw new InvalidOperationException("Unsupported extension");
            }
            volumeStream = inputStream;
            return volumeStream;
        }


Now, one question is left... how can I adjust the volume of the playback? I'm sure it is in the documentation, but I just can't find it :S

And if I use this code to playback a file. How can i check if the playback is finished?

Coordinator
Jan 4, 2011 at 10:17 AM

WaveChannel32 has a volume property. 1.0 is full volume

Mark