如何从 Properties.Resources
播放 wav 文件?
我尝试了一些代码,但每次我的 Priperties.Resource.myFile
都放入 byte[]
但我的代码需要 string
路径,不是 byte[]
数组。
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = @"myFile.wav";
player.Play();
我不想使用某些临时文件。可以直接从资源播放吗?
感谢您的建议!
最佳答案
据我所知,有两种方法可以做到这一点,见下文:
使用文件路径
首先将文件放在项目的根文件夹中,然后无论您在Debug
或Release
模式下运行程序,都可以肯定地访问该文件。var basePath = System.AppDomain.CurrentDomain.BaseDirectory; SoundPlayer player = new SoundPlayer(); player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav"); player.Load(); player.Play();
使用资源
按照下面的动画,将“现有文件”添加到项目中。
SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);
player.Play();
这种方式的优点是:
运行程序时只需复制“bin”目录下的“Release”文件夹即可。
关于c# - 如何使用 C# 从 Properties.Resources 播放 WAV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35117195/