我正在C#上编写控制台应用程序,并且想在连续显示文本时播放声音。这是我所做的:
static SoundPlayer typewriter = new SoundPlayer("typewriter");
static public void print(string str, int delay)
{
Thread skipThread = new Thread(skipText);
typewriter.PlayLooping();
textgap = delay;
foreach (char c in str)
{
Console.Write(c);
if (textgap != 0)
Thread.Sleep(textgap);
}
typewriter.Stop();
}
typewriter.wav
已导入到.cs
文件旁边的我的项目中,并且我已选择copy always
。当我运行此代码时,开始播放声音时弹出错误提示“Please be sure a sound file exists at the specified location.
这是怎么了?”
编辑:根据Kevin J的回答,将我的代码更改为以下代码。
static SoundPlayer typewritter;
public static void Load()
{
Assembly assembly;
assembly = Assembly.GetExecutingAssembly();
typewritter = new SoundPlayer(assembly.GetManifestResourceStream
("typewriter"));
}
我还应该精确地使用路径,Environment.CurruntDirectory + "typewriter"
但没有任何变化。
解决问题:我只需要设置实例的SoundLocation
属性SoundPlayer
:
SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句