我正在 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"
,但没有任何变化。