-1

I'm making a program in Visual Studio 2015 (C#) and I want to add sound effects to it. However, I have looked up countless tutorials but none of them seem to work, and gave me tons of errors. If anyone can give me a code to play a .wav file from resource files then I would be very grateful

Artem Kulikov
  • 2,250
  • 19
  • 32
Catquatwa
  • 1,729
  • 3
  • 12
  • 15

4 Answers4

0

If the file you want to play is wav files, try this.

var player = new System.Media.SoundPlayer("c:\\tes.wav");
player.Play();
Han
  • 3,052
  • 2
  • 22
  • 31
0

How to: Play Sounds in an Application

Add the following method code under the button1_Click event hander :

 System.Media.SoundPlayer player = 
 new System.Media.SoundPlayer();
 player.SoundLocation = @"C:\Users\Public\Music\Sample Music\xxxx.wav";
 player.Load();
 player.Play();
Tharif
  • 13,794
  • 9
  • 55
  • 77
0

For myself I wrote this SounceController, hope it help:

using System.Windows.Media; // add reference to system.windows.presentation.
using System;
using System.IO;
public class SoundController
{
    private bool isPlaying;
    private MediaPlayer player;

    public SoundController()
    {
        player = new MediaPlayer();
    }
    ~SoundController()
    {
        player = null;
    }

    public void Play(string path)
    {
        if (!File.Exists(path) || isPlaying)
            return;

        isPlaying = true;

        player.Open(new Uri(path));
        player.Play();
    }
    public void Stop()
    {
        if (isPlaying)
        {
            isPlaying = false;
            player.Stop();
        }
    }
}
Artem Kulikov
  • 2,250
  • 19
  • 32
  • Did you add a reference to System.Windows.Presentation? Also you need System and IO namespaces. I updated my answer. – Artem Kulikov Aug 04 '15 at 16:21
  • 1
    @Catquatwa Please describe the errors you are having. It's going to be really difficult to offer an error free code without even seeing a sample of your code, or the problems you are encountering. – Matias Cicero Aug 04 '15 at 16:22
0

I recommend you use PInvoke To play sound using winmm.dll

first of all import System.Runtime.InteropServices namespace in to your project.

using System.Runtime.InteropServices;

Then in your class you will have

[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);

public void Play(string path ,string name)
{
     // Open
     mciSendString($@"open {path} type waveaudio alias {name}", null, 0, IntPtr.Zero);
     // Play
     mciSendString($@"play {name}", null, 0, IntPtr.Zero);
 }

You can play the sound sending correct path of wave file with name. . given name does not need to be the same name of wave file.for example:

Play(@"C:\soundeffect.wav", "soundEffect1");

Usually sound effects are played simultaneously. you can call this method several times to play several files simultaneously.

Play(@"C:\soundeffect1.wav", "soundEffect1");
Play(@"C:\soundeffect2.wav", "soundEffect2");
Play(@"C:\soundeffect3.wav", "soundEffect3");
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118