MrME
MrME

Reputation: 337

Song doesn't play all the time in Monogame

Heey,

We created a game with Monogame but we got the following problem.

We got a themesong that plays when you have loaded the game now is the problem that the themesong sometimes plays but sometimes just doesn't. We convert it by the XNA pipeline to a wma and load it into our game with the .xnb together but just sometimes the music doesn't wanna start.

We just use the standard code for starting a song and all of this code does fire.

internal void PlayBackgroundMusic()
{
    if (MediaPlayer.GameHasControl)
    {
        MediaPlayer.Volume = mainVolume;
        MediaPlayer.Play(backgroundMusic);
        MediaPlayer.IsRepeating = true;
    }
}

We also use SoundEffects but these work 100% of the time it's only the song that won't play everytime you start. Windows RT runs it fine by the way.

Upvotes: 1

Views: 2764

Answers (2)

pinckerman
pinckerman

Reputation: 4213

If you want to play a song, use the Song class. But make sure you are using ".mp3" instead of ".wma" before converting them into ".xnb".

backgroundMusic = Content.Load<Song>("MainTheme");
MediaPlayer.Play(backgroundMusic);
MediaPlayer.IsRepeating = true;

See MSDN.

Upvotes: 0

Measurity
Measurity

Reputation: 1346

Make sure that the debugger gets into the if statement through debugging (or remove the statement temporarily). Another possibility might be that the function is running before the game is fully initialized. You could try delaying the function until the game has been fully loaded.

PS: I can't comment on questions yet so here's an answer.

Edit:

Alright, after some messing around with the Song class and looking in the implementation in MonoGame I came to the conclusion that the SoundEffect class is easier to use and better implemented.

backgroundSound = Content.Load<SoundEffect>("alarm");

protected override void BeginRun()
{
    // I created an instance here but you should keep track of this variable
    // in order to stop it when you want.
    var backSong = backgroundSound.CreateInstance();
    backSong.IsLooped = true;
    backSong.Play();

    base.BeginRun();
}

I used this post: using BeginRun override to play the SoundEffect on startup.

Upvotes: 3

Related Questions