博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UWP 播放声音文件
阅读量:4677 次
发布时间:2019-06-09

本文共 2714 字,大约阅读时间需要 9 分钟。

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 

public enum SoundEfxEnum {
NORMALMOVE, CHECK, CAPTURE, CASTLING, } public class SoundEffects {
private Dictionary
effects; public SoundEffects() {
effects = new Dictionary
(); 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
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; /// /// 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(). /// 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

你可能感兴趣的文章
Ubuntu 中sendmail 的安装、配置与发送邮件的具体实现
查看>>
时隔2月,我的第二篇
查看>>
[导入]C++ OpenGL底层和C# GUI无缝联合!
查看>>
调试程序Bug-陈棚
查看>>
STM32 寄存器库和固件库
查看>>
第11周表格
查看>>
linux运维云计算课程学习,Linux云计算面试时遇到的问题
查看>>
Abiword对话框资源
查看>>
跟我一起写 Makefile
查看>>
C# uri
查看>>
GPS定位 测试
查看>>
前端使用AngularJS的$resource,后端ASP.NET Web API,实现增删改查
查看>>
探索从 MVC 到 MVVM + Flux 架构模式的转变
查看>>
传统认知PK网络认知 刚子扯谈烤串认知
查看>>
字节数组java加密与解密
查看>>
矩形运算
查看>>
php 备份mysql数据库(joomla数据库可直接使用,其他数据库稍作修改即可)
查看>>
使用HttpSessionListener接口监听Session的创建和失效
查看>>
20181029 T2 寻宝游戏
查看>>
C++变量作用域、生存期、存储类别
查看>>