Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working with a XNA game and I want my program to load music files from libary/music so I can play that music in the game. It would be great if the mediaplayer class could play a playlist of all the music in library/music.

share|improve this question

Generally, in order to play an MP3 in XNA it has to go through the Content Pipeline. If you want to learn how to do it, a quick search turned up this tutorial.

Now, to your question:

Single Song

To play an MP3, you need to pass an instance of a Song to MediaPlayer.Play. As I stated above, you usually get the instance of your song via the Content Pipeline, but I do see it has a FromUri method.

I have never used it before, and it may only work for the Silverlight framework on Windows Phone, but you can give it a try.If I had to guess, I would assume it would work like this. Keep in mind that the file will have to exist. If it doesn't you will get an error. If you are distributing this game, Use the Content Pipeline. It is much better.

Song song = Song.FromUri(new Uri("file:///C:/Users/Joe/Music/FinalCountdown.mp3"));

I see there was a question on StackOverflow, and the answer looks like he is on a Windows PC. Maybe that will help get you there.


You mention wanting a playlist, well MediaPlayer.Play can also take a SongCollection, and it will just loop through each song.

share|improve this answer
This helps me a little, but what i want to do is that the program shows me what music is in the file and wich music from that file i want to play. – Eric Nilsson Apr 25 '12 at 17:13
Well, use the System.IO.Directory class to get a list of all files in the directory. Once you have them you can easily lay them out on the screen and have the user select one. You have the filepath now, so just turn it into a URI and use the method above. – Joe Apr 26 '12 at 16:12
I have used the System.IO to make a txt file to for example highscore saving, but i haven't used it any further so could you tell me how to use it? – Eric Nilsson Apr 29 '12 at 18:23
Thank you very much :) – Eric Nilsson May 3 '12 at 16:00

As a follow up on this other question you can load all the mp3s in a directory into a Song list with:

var songs = Directory.EnumerateFiles(directory, "*.mp3")
            .Select(file => Song.FromUri(file, new Uri(file)))

You can look in the Music folder by setting directory to the following:

string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);

There's also a known bug where Song.FromUri will throw an exception when trying to load a file that has spaces in its name. See this answer on stackoverflow for a workaround using reflection to call the internal constructor directly.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.