How can I tell when MediaPlayer is finished playing a song?

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;
    }

}

I’m gonna assume that you have the same problem with playing music, as I did with playing videos. I just used a simeple timer to check if video ended, here:

float countDuration = 0f; // 1f = 1 second
float currentTime = 0f;

void LoadContent()
{
    countDuration = song.Duration.Seconds;
}

void Update()
{
    currentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
    if (currentTime >= countDura;tion)
    {
	    //do something
    }
}

There is no media ended event. You have to detect it manually. Beware that platforms act differently. WP pauses at the start of the song, iOS stops at the endmof the song, and so on.