This project has moved. For the latest updates, please go here.

Using MP3 from project resources

Aug 15, 2015 at 1:02 AM
I have made a small little software as practice, where I used .wav files at first. They were way to big and using mp3 would shrink the files by up to 80%. Now, using the code displayed on the FAQ worked fine;
            IWavePlayer waveOutDevice;
            AudioFileReader audioFileReader;
            waveOutDevice = new WaveOut();
            audioFileReader = new AudioFileReader("test.mp3");
            waveOutDevice.Init(audioFileReader);
            waveOutDevice.Play();
However, changing the audioFileReader to;
            audioFileReader = new AudioFileReader(Properties.Resources.test);
would give an exception saying it cannot convert System.IO.UnmanagedMemoryStream to a string.
So I went on Google and found some examples, which I could not get working and currently I have in my project this:
private void clickButton(object sender, RoutedEventArgs e)
        {
            var song = Properties.Resources.test;
            WaveOut = new WaveOut();
            StreamResourceInfo resource = Application.GetResourceStream(new Uri("test.mp3", UriKind.Relative));
            WaveStream MainOutputStream = CreateInputStream(resource.Stream);

        }
        public WaveStream CreateInputStream(byte[] resource)
        {
            WaveChannel32 inputStream;

            using (MemoryStream ms = new MemoryStream(resource))
            {
                WaveStream mp3Reader = new Mp3FileReader(ms);
                inputStream = new WaveChannel32(mp3Reader);
            }

            return inputStream;
        }
It did not work if I replaced test.mp3 with either the var "song", or the Properties.Resources.test.
It gives an exception on this line where it says resource.Stream cannot be converted from IO.Stream to Byte array.
WaveStream MainOutputStream = CreateInputStream(resource.Stream);
How would I go on solving this? I would very much prefer having the files inside the exe file for portability instead of having folders required.
Aug 24, 2015 at 12:54 PM
Edited Aug 24, 2015 at 12:55 PM
AudioFileReader is just a container class that uses Mp3FileReader, WaveFileReader etc. based on the filename,
[if filename.endswith(".mp3")] and so on....
So, actually it doesn´t seem to support streams and you have 2 possibilities to choose:

1) Copy the AudioFileReader.cs class from NAudio source and adjust it for stream support
2) Assign readers yourself, as I have done e.g. here: Url

I´d recommend using option 1, as it is an easier approach for beginners. You also have to look up if your used readers can handle streams, e.g. Mp3FileReader can read from a stream.