Reputation: 209
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.
Upvotes: 1
Views: 1986
Reputation: 2062
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.
Upvotes: 1
Reputation: 1525
Just add it to the project. Right click on the project => Add => Existing Item... pick the file from the file picker. It will be sent to the output folder on compilation.
Upvotes: 0