在Windows Phone 中播放提示音可以使用 Microsoft.Xna.Framework.Audio 命名空间下的 SoundEffect 类。具体使用方法如下:
1. 根据声音文件路径创建播放实例。
private void InitSoundEffact() { var stream = TitleContainer.OpenStream("Resources/chat.wav"); soundEffact = SoundEffect.FromStream(stream); }
2. 调用 Play() 方法播放声音。
/// <summary> /// 播放声音 /// </summary> public void Play() { if (soundEffact == null) { return; } FrameworkDispatcher.Update(); soundEffact.Play(); }
需要注意的是:
1. 声音文件只支持wav类型;
2. 在调用 Play() 方法之前一定要调用 FrameworkDispatcher.Update() 方法。
源代码:demo