【问题标题】:Why is my background music not looping?为什么我的背景音乐没有循环播放?
【发布时间】:2013-12-02 14:25:32
【问题描述】:

有人知道为什么这段代码不能让我的背景音乐不断循环吗??

正在加载内容:

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

【问题讨论】:

    标签: c# loops xna audio


    【解决方案1】:

    如果你需要管理背景音乐,你应该使用Song类,SoundEffect应该只用于音效。
    像这样的:

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

    当然加载应该是这样的:

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

    【讨论】:

    • 请看上面的代码编辑,我认为你的意思是这样的吗?
    • 没有。您仍在使用SoundEffectSoundEffectInstance,但如果您想管理音乐,您应该使用Song 类。
    • 对不起,我没有正确阅读答案。再次编辑它。
    • 你在做SoundEffectInstance instance = backgroundMusic.CreateInstance();,这里假设backgroundMusicSoundEffect,那怎么可能也是Song呢?
    • 请检查我更新的代码,这看起来对你有用吗?我现在根本没有音频输出?
    【解决方案2】:

    您正在播放 SoundEffect,但循环 SoundEffectInstance,这是行不通的。 根据 Microsoft (http://msdn.microsoft.com/en-us/library/dd940203.aspx) 的这篇文章,您必须在播放声音之前设置 IsLooped 属性。

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

    在加载内容中:

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

    更新中:

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

    【讨论】:

      猜你喜欢
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      相关资源
      最近更新 更多