How do you play C #?

I tried this, but I'm interested in playing the sound where my program starts from. I have a .wav file inside the project folder.

   SoundPlayer simpleSound = new SoundPlayer(@"/yay.wav");
    simpleSound.Play();

thank

+3
source share
2 answers

I have a .wav file inside the project folder.

This is probably your problem.

When you compile your application, it does not end right in the project folder - it ends in a subdirectory (either /Debug/bin, or /Release/bin). Put the wav file there, not in the project directory and see how it works.

+3
source

Before you start playing sound, you should be familiar with the PlaySound () function of the Win32 API.

private SoundPlayer player = new SoundPlayer();

/// Button click event handler.
private void AsyncBtn_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Set .wav file as TextBox.Text.
        textBox1.Text = openFileDialog1.FileName;

        // Add LoadCompleted event handler.
        player.LoadCompleted += new AsyncCompletedEventHandler(LoadCompleted);

        // Set location of the .wav file.
        player.SoundLocation = openFileDialog1.FileName;

        // Load asynchronously.
        player.LoadAsync();
    }
}

/// LoadCompleted event handler.
private void LoadCompleted(object sender, AsyncCompletedEventArgs args)
{
    player.Play();
}
+1
source

Source: https://habr.com/ru/post/1779531/


All Articles