How to play an Audio File in Silverlight, Part 2

I was asked in a comment how to play a sound from a stream, from an embedded resource in Silverlight or WPF.

My answer is that I wouldn’t use an embedded resource, instead I’d suggest using the “Content” Build Action as shown below:

image

I’ve set the explosion.mp3 file’s Build Action to “Content”. Then, using my audio playing technique, I call the function:

PlayAudio("/explosion.mp3", .5);

(The .5 sets the volume.)

You might look at MediaStreamSource (set via the MediaElement’s SetSource property).

There’s an example dealing with MP3 files here. Realize that path requires that your code fully take over the the responsibility for streaming — including understanding the low-level details of the audio/video format. For example, you would need to know how to decode an MP3 file.

2 Comments

Comments are closed.