XNA MediaPlayer downloads music

I load music with the following code into the content download function:

song = Content.Load<Song>("music/game"); MediaPlayer.IsRepeating = false; MediaPlayer.Play(song); 

there is nothing strange, but each round in my game lasts 2 minutes and should synchronize with the music (it lasts 2 minutes), but the music ends between 2-4 early. This would not be a problem if it was always the same time.

I assume this has something to do with load times? any advice?

+4
source share
1 answer

One thing you can do is move the Content.Load<Song> method to Load and check if it plays in the update, and if not, play. For instance,

 public void LoadContent(ConentManager content) { song = content.Load<Song>("music/game"); gameSongStartedPlaying = false; // this variable to hold if you have starting playing this song already MediaPlayer.IsRepeating = false; } public void Update(GameTime gameTime) { if(MediaPlayer.State == MediaState.Stopped && !gameSongStartedPlaying) { MediaPlayer.Play(song); gameSongStartedPlaying = true; } } 

This should start playing the song on the first pass of the Update method, and not at the download stage, when the song is β€œplaying”, and all resources after Content.Load<Song> are still loading (this would be the reason that your song ends earlier).

+1
source

All Articles