Rich
Rich

Reputation: 15

Why is my background music not looping?

Dose anyone have any idae why this code is not working for my background music to constantly loop??

In load Content:

backgroundMusic.Play(0.3f, 0.0f, 0.0f); //play background music (first float number is for volume)

In update:

SoundEffectInstance instance = backgroundMusic.CreateInstance(); //creates instance for backgroundMusic
instance.IsLooped = true; //states that instance should loop meaning background music loops and never stops

Thanks in advance

Edit: I now have this:

Content Load:

        Song backgroundMusic = Content.Load<Song>("backgroundMusic");

and then seperately:

    public void PlayMusicRepeat(Song backgroundMusic)
    {

        MediaPlayer.Play(backgroundMusic);
        MediaPlayer.IsRepeating = true;
    }

Upvotes: 0

Views: 1501

Answers (2)

Kai Hartmann
Kai Hartmann

Reputation: 3154

You are playing the SoundEffect, but looping the SoundEffectInstance, that won't work. And following this article from Microsoft (http://msdn.microsoft.com/en-us/library/dd940203.aspx), you have to set the IsLooped property BEFORE playing the sound.

So your code should look like this:

In LoadContent:

instance = backgroundMusic.CreateInstance(); 
instance.IsLooped = true;

In Update:

if(instance.State == SoundState.Stopped)
    instance.Play();

Upvotes: 1

pinckerman
pinckerman

Reputation: 4213

If you need to manage the backgroud music you should use the Song class, SoundEffect should be used only for sound effects.
Something like this:

public void PlayMusicRepeat(Song song)
{
    MediaPlayer.Play(song);
    MediaPlayer.IsRepeating = true;
}

Of course the loading should be like this:

Song music = Game.Content.Load<Song>("background");

Upvotes: 3

Related Questions