Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a program that plays a song when you click a button. The Program works great.. Except the song is read from my hard drive. When I add the song as a resource on my project and change the path then I get an error.

I've been away from C# for a while since I started working at a web development company using PHP. So I need someone to just give me a quick catch up on what I'm doing wrong.

This is my working code:(Code reading song from hard drive)
C#
private void btnPlay_Click(object sender, EventArgs e)
        {
            wmpPlay.URL = @"c:\song.mp3";
        }


This is the one that receives the error:
C#
private void btnPlay_Click(object sender, EventArgs e)
        {
            wmpPlay.URL = @"../Resources/song.mp3";
        }


Thanks in advance,
Chris
Posted

Paste your song inside your application bin/Debug folder

and write this code on play button click event

C#
wmpPlay.URL = Application.StartupPath + @"/song.mp3";


Thanks & regard
Sham
 
Share this answer
 
Comments
Christopher Smit 1-Jun-13 7:22am    
Thank you.. :) I did that except I did not write the "Application.StartupPath +" part... :)

Thanks for your time,
Chris
Shambhoo kumar 3-Jun-13 7:22am    
You must welcome dear Christopher Smit .
to get the resource you do something like this

System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly();
objStream = objAssembly.GetManifestResourceStream("My.Namespace.song.mp3");


Presumably song.mp3 has been added correctly and flagged as an Embedded Resource on compilation

Note the Namespace !!

From StackOverflow, I found this :-

C#
string[] myResources = objAssembly.GetManifestResourceNames();
foreach(string aresource in myResources) {
   Console.WriteLine(aresource);
}


to print the names of the resources

Ive seen a couple of suggestions where to go from there - either writing the resource from objStream to a temporary file and playing it from disk, or using ?'WaveOut' classes to play it directly (which is what I'd do)

'g'
 
Share this answer
 
Comments
Christopher Smit 1-Jun-13 7:05am    
Thank you for the answer. This works! :) However I found a faster and easier way. I do not know if this is good coding practice, but i just threw the song in the bin folder and changed my URL to @"song.mp3"

Worked like a charm.. lol I might receive some errors in future, to which case I will fall back to your suggestion. :)

Thank you for your time,
Chris

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900