I'm writing a Console application on C# and I want to play a sound when I display texts continuously. This is what I've done :
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
is imported to my project next to the .cs
files and I've selected copy always
. When I run this code, an error pops out when starting playing the sound saying Please be sure a sound file exists at the specified location.
What is wrong here?
EDIT : Changed my code to the following according to Kevin J's answer.
static SoundPlayer typewritter;
public static void Load()
{
Assembly assembly;
assembly = Assembly.GetExecutingAssembly();
typewritter = new SoundPlayer(assembly.GetManifestResourceStream
("typewriter"));
}
I also should precised to use the path Environment.CurruntDirectory + "typewriter"
but nothing changes.
SoundPlayer player = new SoundPlayer("C:\\bass.wav"))
. Your code above doesn't appear to have a full path. – Flattoassembly.GetManifestResourceStream
. It should look something like "Yournamespace.typewriter.wav". You only have "typewriter". You MUST qualify the resource with the namespace of your app and the correct file name. "typewriter" is not a correct name for a .wav file. – Flatto