1
\$\begingroup\$

The current tutorial I am using to play background songs only allows me to play one song throughout the entire game.

Link to tutorial --> http://rbwhitaker.wikidot.com/playing-background-music

I want to have multiple songs where after one song is finished playing another random song will be selected and played.

How would I do that?

Also I would not liked to get involved in XACT.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

For checking if a song has finished playing, do this:

if(MediaPlayer.State != MediaState.Playing 
   && MediaPlayer.PlayPosition.TotalSeconds == 0.0f){
  //start playing new song
}

For starting a random song, you should have a list of songs:

public List<Song> bgm = new List<Song>();

Then playing a random one could be like this (if your list is not empty):

MediaPlayer.Play(bgm[rand.Next(bgm.Count)]);

Keep in mind that selecting a random song like this will not guarantee that the same is not played multiple times one after another. You should shuffle your list of songs for that and selecting the next song each time.

\$\endgroup\$
5
  • \$\begingroup\$ While this does work I read that checking MediaPlayer.State is very slow. Is that the only way to check if the song is done playing? \$\endgroup\$
    – vroomvsr
    Apr 25, 2015 at 13:10
  • \$\begingroup\$ I don't know of any, but I doubt it would slow you down. \$\endgroup\$ Apr 25, 2015 at 13:26
  • \$\begingroup\$ Where would I put the code you suggested, would it be in the update method? I did a simple test by writing the code MediaPlayer.Play(song5) and while it worked fine when I put it at the end of the loadcontent method it would not work when I put it in the update method (it would just be silent). \$\endgroup\$
    – vroomvsr
    Apr 25, 2015 at 17:07
  • \$\begingroup\$ It would go in the update method, but make sure it is only called once. If you write it without conditions, it would trigger Medaiplayer.Play() every update, so it would be stuck on the beginning of the song forever. \$\endgroup\$ Apr 25, 2015 at 22:20
  • \$\begingroup\$ Actually this doesn't work as expected. After a long period of time all the music stops playing. I did a test and noticed that for some reason the Media Player thought it was still in the "Playing" state. \$\endgroup\$
    – vroomvsr
    May 2, 2015 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .