Hi,
I am setting up background music for my game. I am able to get a song to play, but when the song is complete I would like a second song to play. How can I tell when MediaPlayer is finished playing a song?
I have tried adding MediaPlayer.MediaEnded += delegate { MessageBox.Show(“Media Ended”); }; hoping to catch the MediaEnded event, but it says that ‘MediaPlayer does not contain a definition for MediaEnded’.
I also tried adding a loop that watches for this condition: MediaPlayer.PlayPosition==new TimeSpan(0,0,0). For some reason, this condition will fire when the game first starts (regardless of if MediaPlayer.play() has occured) but it will not fire after the song is complete, although MediaPlayer.PlayPosition reads “00:00:00”.
Here is my class (I left out the event and the loop since they were not working):
public class BackgroundMusic
{
List songs= new List();
int active = 0;
public BackgroundMusic(ContentManager Content)
{
// load the files
songs.Add(Content.Load<Song>("instrumental2"));
songs.Add(Content.Load<Song>("instrumental5"));
// play files
MediaPlayer.Volume = 0.75f;
MediaPlayer.Play(songs[active]);
MediaPlayer.IsRepeating = false;
}
}