UWP 播放声音文件

I finally found something that helps accomplish what I want.  I have taken what Diederik Krols has posted a year ago and adapted for my use.  He describes using a singleton class but I find I don't need singleton for my purpose.  I created a custom class to load up the sounds and then I declare static variable in App.xaml.cs and instantiate it there.  This makes the sounds globally available by referencing App.MyAppSounds.  I'm willing to share, here is my soundeffects class and how I use it adapted from this article Playing Sounds in a Universal Windows MVVM App

 public enum SoundEfxEnum { NORMALMOVE, CHECK, CAPTURE, CASTLING, } public class SoundEffects { private Dictionary<SoundEfxEnum, MediaElement> effects; public SoundEffects() { effects = new Dictionary<SoundEfxEnum, MediaElement>(); LoadEfx(); } private async void LoadEfx() { effects.Add(SoundEfxEnum.NORMALMOVE, await LoadSoundFile("normalmove.wav")); effects.Add(SoundEfxEnum.CHECK, await LoadSoundFile("check.wav")); effects.Add(SoundEfxEnum.CAPTURE, await LoadSoundFile("capture.wav")); effects.Add(SoundEfxEnum.CASTLING, await LoadSoundFile("castling.wav")); } private async Task<MediaElement> LoadSoundFile(string v) { MediaElement snd = new MediaElement(); snd.AutoPlay = false; StorageFolder folder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("sounds"); StorageFile file = await folder.GetFileAsync(v); var stream = await file.OpenAsync(FileAccessMode.Read); snd.SetSource(stream, file.ContentType); return snd; } public async Task Play(SoundEfxEnum efx) { var mediaElement = effects[efx]; await mediaElement.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { mediaElement.Stop(); mediaElement.Play(); }); } }


In App.xaml.cs I declare the variable:

 sealed partial class App : Application { public static SoundEffects MyAppSounds; /// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; }

In OnLaunched in App.xaml.cs at end just before Window is activated, I instantiate the static variable

 // My Application Sound Effects MyAppSounds = new SoundEffects(); // Ensure the current window is active Window.Current.Activate(); }

To use it, I do this anytime I want to play the sound

 

// Play a sound effect App.MyAppSounds.Play(SoundEfxEnum.Check); // Or asynchronously await App.MyAppSounds.Play(SoundEfxEnum.Check);

 

 文字地址: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/ddb1b7f1-e988-40c7-8e1e-eaf6d8573ec2/uwp-how-to-play-sound-from-wav-fileresource?forum=wpdevelop

 

转载于:https://www.cnblogs.com/wgscd/articles/7705041.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值