在控制台上播放声音 - C#

use*_*830 5 c# audio console console-application soundplayer

我正在 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();

    }
Run Code Online (Sandbox Code Playgroud)

typewriter.wav导入到我的项目旁边的.cs文件中,我选择了copy always. 当我运行此代码时,开始播放声音时会弹出一个错误,说Please be sure a sound file exists at the specified location. 这里有什么问题?

编辑:根据凯文 J 的回答将我的代码更改为以下内容。

static SoundPlayer typewritter;

    public static void Load()
    {
        Assembly assembly;
        assembly = Assembly.GetExecutingAssembly();
        typewritter = new SoundPlayer(assembly.GetManifestResourceStream
            ("typewriter"));
    }
Run Code Online (Sandbox Code Playgroud)

我也应该精确使用路径Environment.CurruntDirectory + "typewriter"但没有任何变化。

use*_*830 5

找出问题:我只需要设置实例的SoundLocation属性SoundPlayer

SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
Run Code Online (Sandbox Code Playgroud)