本文介绍了SoundEffect 类 Windows Phone 8 中的循环声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款游戏,需要为此添加背景音乐.我尝试了 Microsoft.Xna.Framework.Audio 命名空间的 SoundEffect 类.

I am developing a game, and need to add background music for that. I tried Microsoft.Xna.Framework.Audio namespace's SoundEffect class.

最初我使用

SoundEffectInstance Sound = 
     SoundEffect.FromStream(Application.GetResourceStream(new Uri("Assets/background.wav", UriKind.Relative)).Stream).CreateInstance();
Sound.IsLooped = true;
Sound.Play();

它不起作用.然后我尝试了

And it was not working. Then I tried

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();

它的工作原理.但是不能循环播放音乐.任何人都可以向我描述这些差异并建议一种循环播放音乐的方法.

And its working. But Cant loop the music. Can anyone please describe me the differences and suggest a way to loop the music please.

我想称之为`public MainPage() { }

Edit : I wanna call this is `public MainPage() { }

更新:我通过在委托中添加它来使它工作,如下所示

Update : I made it work by adding this in delegates as below

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 秒.如何克服这个问题.

Now I have another problem that, the audio file duration is 2mins but the above code plays the music only for 30 seconds. How to overcome this issue.

推荐答案

大功告成.SoundEffect 类确实不支持循环.因此,您需要 SoundEffectInstance 类.您可以基于您已经创建的 SoundEffect-instance 创建此类的实例:

You're almost there. The SoundEffect class does indeed not support looping. Therefore you need the SoundEffectInstance class. You can create an instance of this class based on your already created SoundEffect-instance:

//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)

这篇关于SoundEffect 类 Windows Phone 8 中的循环声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 05:52