为什么我的背景音乐没有循环?

时间:2013-12-02 14:25:32

标签: c# loops xna audio

任何人都有任何idae为什么这段代码不适合我的背景音乐不断循环?

载入内容:

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

更新:

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

提前致谢

编辑:我现在有这个:

内容加载:

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

然后分开:

    public void PlayMusicRepeat(Song backgroundMusic)
    {

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

2 个答案:

答案 0 :(得分:2)

如果您需要管理背景音乐,则应使用Song课程,SoundEffect仅应用于音效。
像这样:

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

当然加载应该是这样的:

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

答案 1 :(得分:1)

您正在播放SoundEffect,但循环SoundEffectInstance,这将无法正常工作。 遵循Microsoft(http://msdn.microsoft.com/en-us/library/dd940203.aspx)的这篇文章,您必须在播放声音之前设置IsLooped属性。

所以你的代码应该是这样的:

在LoadContent中:

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

更新:

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