我有一个负责扫描条形码的 Compact Framework 3.5 应用程序。根据情况,它应该播放 3 种不同的声音,因此我围绕 SoundPlayer 对象创建了一个包装类,并在其上调用 Play 方法。
public static class SoundEffectsPlayer
{
private static readonly SoundEffect _alert;
static SoundEffectsPlayer()
{
// This will cause the SoundEffect class to throw an error that the Uri
// Format is not supported, when new SoundPlayer(location) is called.
_alert = new SoundEffect("SoundEffects/alert.wav");
}
public static SoundEffect Alert
{
get { return _alert; }
}
}
public class SoundEffect
{
private readonly SoundPlayer _sound;
public SoundEffect(string location)
{
_sound = new SoundPlayer(location);
_sound.Load();
}
public bool IsLoaded
{
get { return _sound.IsLoadCompleted; }
}
public void Play()
{
_sound.Play();
}
}
这个想法是不要在每次需要扫描条形码时创建 SoundPlayer(每小时扫描几百次)。所以我可以在已经加载的文件上调用 Play 。
alert.wav 文件位于应用程序引用的库项目的根目录中的 SoundEffects 文件夹中,它被设置为嵌入式资源。我需要将什么传递给 SoundEffects 类来加载 wav 文件?wav 文件是否应该嵌入到库项目中?
还有人注意到我处理 wav 文件播放的方式有什么问题吗?这是我第一次尝试这样的事情,所以我愿意接受改进的建议。