Playing MP3 from resource file

DoJa

Active member
Joined
Mar 23, 2014
Messages
33
Programming Experience
1-3
I'm using the following code to play an mp3 file.

C#:
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "c:\\sample.mp3";
            wplayer.controls.play();

This works great but requires an absolute path to the file which is undesirable.

I tried changing the 2nd line to
C#:
wplayer.URL = Properties.Resources.sample
but this gives a "Cannot implicitly convert type 'byte[]' to 'string'" error and adding .tostring() does not make things any better.

How can I get it to play a mp3 in the resource file ?
 
Of course that doesn't work. The URL property takes the path to a file. Does your resource contain the path to a file?

I doubt that you can play a resource directly, although I don't know that for sure. You would probably have to save the data to a file - which can be done with File.WriteAllBytes - and then do exactly what you did in the first example.

That said, have you looked at what other members that WindowsMediaPlayer object has? I don't know because I haven't used it myself but, if I wanted to, I'd look through the available properties and see if there was one that looked like it would accept a Byte array or Stream or something similar.
 
You would probably have to save the data to a file - which can be done with File.WriteAllBytes - and then do exactly what you did in the first example.

That said, have you looked at what other members that WindowsMediaPlayer object has?


I did look for other members and did not spot anything obvious. Saving the data to a file makes sense but seems a little overcomplicated for what ought to be a simple operation.

I am just making a very small application as a means of learning how to use the WPF framework and was hoping to keep the few notification sound clips and any related imagery embedded in the .exe so as not to need separate folders and such for the application to work properly.
 
Seems like wav files are the way forward but finding out how to play them from a resource file rather than a url was harder to track down.

For the benefit of anyone else looking for a solution here is how to play a wav from a resource in c#

C#:
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.sound;
player.Play();

Quick question for those in the know, if I were to put the above code, in a private void and called it every time I wanted the sound to play, would it keep creating new 'player' objects and waste memory, or would the previous one be replaced/overwritten by the new one?

Wondering if it would be more efficient to only call the above two lines once and then the last line each time the sound was required.
 
That code is going to create a new SoundPlayer object and a new Stream object each time. Whether or not you should execute it multiple times depends on the specific circumstances. There may be times where it is more appropriate to create new objects and there may be times where it is more appropriate to reuse existing objects.
 
Back
Top Bottom