Question

I'm trying to get background music going for my XNA game, but the options in XNA all require that the audio files be converted to .wav and included as project resources. This doesn't work for me for two reasons. First, .wav files are enormous, and have no real benefit to the user. Second, the user would be unable to have a folder of mp3s to substitute, should they prefer their own music. What are my options for playing mp3s that are not actually part of the project?

Was it helpful?

Solution

After doing a lot of searching, I finally found this thread on the App Hub forums which gave me a solution.

You can use Song.FromUri to load a song, then use MediaPlayer.Play(song) to play said song.

Uri songPath = new Uri("Songs/song.mp3", UriKind.Relative);
Song song = Song.FromUri("song", songPath);
MediaPlayer.Play(song);

In your question you said (quite obviously) that you don't want to use MediaPlayer, but it seems as though the reason why is because you would first need to convert the song and it needs to be included in the project resources. With this, the song would only need to be in a "Songs" directory (which would be located in the same directory as the executable).

Using the SoundEffect class wouldn't work either, since you'd have to use TitleContainer.OpenStream, which liked to complain when I tried it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top