Search code examples
c#mp3naudio

Where to put .mp3 file in visual C# project.?


I am designing a small application in visual C# Winforms. I am using a NAudio and playing an .mp3 file. Below is the code:

IWavePlayer waveOutDevice = new WaveOut();
AudioFileReader audioFileReader = new AudioFileReader("ringtone.mp3");

waveOutDevice.Init(audioFileReader);
waveOutDevice.Play();

I have saved this ringtone.mp3 in debug folder. But I think if I use this exe on another system. Then this mp3 will not work. So I want to know where I can save this mp3 file inside the project and the how to use it.


Solution

  • Option 1: You will have to make sure that the mp3 file is always on the same folder as the exe file. Maybe create an installer which will copy both files on the same folder.

    Option 2: Embed the mp3 file inside the exe. Just copy the mp3 inside your Visual Basic solution and set it's Build Actions to Embedded Resource

    Then in your code you will refer to it with this:

    Stream mp3FileStream = Assembly.GetEntryAssembly().GetManifestResourceStream("YourSolution.ringtone.mp3");
    

    This will return a Stream object. You will have to see how to pass that to your IWavePlayer object.

    My guess is something like this waveOutDevice.Init(mp3FileStream), but did not even tested this.