Playing sounds on Console - C#
Asked Answered
W

3

5

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.

Worthy answered 25/2, 2014 at 23:42 Comment(7)
Does it put the file in the same folder as your executable?Fiftieth
@DStanley Yes, it does.Worthy
Unless you've imported the .wav file as a resource, you'll need to specify the exact path of your .wav file for SoundPlayer. i.e. SoundPlayer player = new SoundPlayer("C:\\bass.wav")). Your code above doesn't appear to have a full path.Flatto
Looking at your new code, you are not putting the correct resource identifier for assembly.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
@KevinJ I can't put "mynamespace.typewriter.wav" as parameter to the function just because it says that typewriter is not defined.Worthy
Your code above is not showing the namespace. Is your namespace actually called "mynamespace"?Flatto
@KevinJ It is not called "mynamespace", I just used that as an example. I put the actual namespace on my code.Worthy
W
5

Figured out the problem : I just needed to set the SoundLocation property of the SoundPlayer instance :

SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
Worthy answered 26/2, 2014 at 2:22 Comment(0)
F
3

Here's something that might help you out (please note that this code is for a winforms app, but you should be able to convert to a console app. Just study the code to see how it works) You'll basically be adding the .wav file as a 'resource' to your program. Then, your program can access the .wav file and play it:

enter image description here

using System.Reflection;
using System.IO;
using System.Resources;
using System.Media;
using System.Diagnostics;



namespace Yournamespace
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            Assembly assembly;
            Stream soundStream;
            SoundPlayer sp;
            assembly = Assembly.GetExecutingAssembly();
            sp = new SoundPlayer(assembly.GetManifestResourceStream
                ("Yournamespace.Dreamer.wav"));
            sp.Play();  
        } 
    }
}
Flatto answered 25/2, 2014 at 23:59 Comment(3)
Thanks for your answer, when I this, at least there are no errors but the sound played is just a system beep sound, nothing to do with my original sound.Worthy
Post your new code in your original question as an edit and let's see what you've got.Flatto
Editted my post according to your comments.Worthy
E
0

If for example you have your sounds in the folder "Assets" then subfolder "SoundClips" do it like this.

var soundLocation = Environment.CurrentDirectory + @"\Assets\SoundClips\";

SoundPlayer player = new SoundPlayer
{
    SoundLocation = soundLocation + "typewriter.wav",
};

Make sure you have the file properties set to:

build action - Content

copy to output directory - Copy if newer

Eluviation answered 31/10, 2018 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.