我正在开发一个游戏,需要添加背景音乐。我尝试了Microsoft.Xna.Framework.Audio命名空间的SoundEffect类。
一开始我用
SoundEffectInstance Sound =
SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/background.wav", UriKind.Relative)).Stream).CreateInstance();
Sound.IsLooped = true;
Sound.Play();但它不起作用。然后我试着
SoundEffect sound;
StreamResourceInfo info = Application.GetResourceStream(
new Uri("Assets/background.wav", UriKind.Relative));
sound= SoundEffect.FromStream(info.Stream);
Microsoft.Xna.Framework.FrameworkDispatcher.Update();
sound.Play();而且起作用了。但坎特把音乐循环起来。有谁能描述一下我的不同之处并建议一种循环音乐的方法吗?
编辑:我想称这是` `public (){}
更新:我在代表中添加了以下内容,从而使其工作正常。
public MainPage()
{
InitializeComponent();
startBackgroundMusic();
}
private void startBackgroundMusic()
{
this.Dispatcher.BeginInvoke(() =>
{
StreamResourceInfo info = Application.GetResourceStream(
new Uri("Assets/background.wav", UriKind.Relative));
backgroundMusic = SoundEffect.FromStream(info.Stream);
SoundEffectInstance instance = backgroundMusic.CreateInstance();
instance.IsLooped = true;
instance.Play();
});
} 现在我有另一个问题,音频文件的持续时间是2分钟,但是上面的代码只播放30秒的音乐。如何克服这个问题。
发布于 2014-02-27 14:01:16
你快到了。SoundEffect类确实不支持循环。因此,您需要SoundEffectInstance类。您可以基于已经创建的SoundEffect实例创建该类的实例:
//What you already had:
StreamResourceInfo info = Application.GetResourceStream(new Uri("Assets/background.wav", UriKind.Relative));
SoundEffect sound = SoundEffect.FromStream(info.Stream);
//Here's the magic:
SoundEffectInstance instance = sound.CreateInstance();
instance.IsLooped = true;
instance.Play();更多阅读(MSDN)
https://stackoverflow.com/questions/22070480
复制相似问题