简体   繁体   English

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

[英]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;
    }

If you need to manage the backgroud music you should use the Song class, SoundEffect should be used only for sound effects. 如果需要管理背景音乐,则应使用Song类, SoundEffect仅应用于声音效果。
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");

You are playing the SoundEffect, but looping the SoundEffectInstance, that won't work. 您正在播放SoundEffect,但循环播放SoundEffectInstance,将无法正常工作。 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. 根据Microsoft的这篇文章( http://msdn.microsoft.com/zh-cn/library/dd940203.aspx ),您必须在播放声音之前设置IsLooped属性。

So your code should look like this: 因此,您的代码应如下所示:

In LoadContent: 在LoadContent中:

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

In Update: 在更新中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM