简体   繁体   English

wp7用什么在silverlight播放音效

[英]What to use for playing sound effects in silverlight for wp7

I know I can reference XNA for the SoundEffect class and that's what I've been doing so far but I was wondering if there was a better way than what I've been doing.我知道我可以为 SoundEffect class 参考 XNA,这就是我迄今为止一直在做的事情,但我想知道是否有比我一直在做的更好的方法。

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

using (var stream = TitleContainer.OpenStream("test.mp3"))
{
          var effect = SoundEffect.FromStream(stream);
          FrameworkDispatcher.Update();
          effect.Play();
}

For my test app I have 20 sounds each 1 second long that I want to play once button are pressed.对于我的测试应用程序,一旦按下按钮,我想播放每 1 秒长的 20 种声音。 I'm playing around with different techniques but if possible I'd like to know how professionals go about doing this before I commit in making a sound effect based app.我正在使用不同的技术,但如果可能的话,我想知道专业人士 go 在我致力于制作基于音效的应用程序之前如何做到这一点。 Little things such as loading the sound effect first or loading it the instance the button is pressed would be helpful.诸如首先加载音效或在按下按钮的实例时加载它这样的小事情会很有帮助。

Thanks.谢谢。

I think a good example would be the official sample on AppHub .我认为AppHub上的官方示例就是一个很好的例子。 It demonstrates how to play multiple sounds.它演示了如何播放多种声音。 You can directly download the sample from here .您可以从这里直接下载示例。

This sample demonstrates how to use the XNA Framework's SoundEffect and SoundEffectInstance classes to play multiple sounds simultaneously in a Silverlight application for Windows Phone.此示例演示如何使用 XNA 框架的 SoundEffect 和 SoundEffectInstance 类在 Windows 电话的 Silverlight 应用程序中同时播放多个声音。 It also shows a simple way to set up a DispatchTimer to call FrameworkDispatcher.Update in order to simulate the Game loop for the XNA Framework's internals.它还展示了一种设置 DispatchTimer 以调用 FrameworkDispatcher.Update 以模拟 XNA 框架内部的游戏循环的简单方法。 Finally, it shows how to load a wave audio file into a Stream that can be played by the SoundEffect classes.最后,它展示了如何将波形音频文件加载到可由 SoundEffect 类播放的 Stream 中。

If I were you I would use PhoneyTools SoundEffectPlayer如果我是你,我会使用PhoneyTools SoundEffectPlayer

This class is used to play SoundEffect objects using the XNA integration.此 class 用于使用 XNA 集成播放 SoundEffect 对象。 The player must live long enough for the sound effect to play so it is common to have it scoped outside a method.播放器必须活得足够长才能播放音效,因此通常将其限定在方法之外。 For example:例如:

public partial class MediaPage : PhoneApplicationPage
{
  // ...

  SoundEffectPlayer _player = null;

  private void playButton_Click(object sender, RoutedEventArgs e)
  {
    var resource = Application.GetResourceStream(new Uri("alert.wav", UriKind.Relative));
    var effect = SoundEffect.FromStream(resource.Stream);
    _player = new SoundEffectPlayer(effect);
    _player.Play();

  }
}

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

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