Programming in C#

13. Playing Sound and Music

Visual Studio C# has a few useful functions on playing some simple system sounds and play music files such as WAV and MPEG files, which are commonly used.

13.1 Playing System Sounds

Visual studio can play some simple build in sounds. They include: Asterick, Beep, Exclamation, Hand and Question. They are useful for user interfaces and pop up dialog boxes only.
Select the type from the SystemSounds class and play it with the Play() function.

Example,

// Play a system sound from PlaySound button
public void playSound_Click(object s, EventArgs e)
{
SystemSound snd;
snd = SystemSounds.Exclamation;
snd.Play();
}

13.2 Playing Wave files

Wave files are basic uncompressed sound or music files. You can find some wave files in C:\Windows\Media folder. This is bigger selection than the the System Sounds but you can also play sampled sounds which are sometimes stored as Wave files. There are plenty of wave sound files you can download from the internet e.g FreeSite.com and Free Wave Samples for instruments.

To play wave files use the SoundPlayer class, set the Location to the full path of the sound filename and use the Play() function to play it. Use PlayLooping() to play it over and over and use the Stop() function to stop playing.

public void playWave_Click(object s, EventArgs e)
{
FileDialog fileDg = new OpenFileDialog();
fileDg.Filter = "WAV files (*.wav)|*.wav|All files (*.*)|*.*";
fileDg.ShowDialog();
string file = fileDg.FileName;

if (file.Length != 0)
  {
    SoundPlayer snd = new SoundPlayer();
     snd.SoundLocation = file;
     snd.Play();


  }
}

13.3 Playing Media Files

Media files can be MPEG files although Windows Audio (WMA) files can also be played using the MediaPlayer class. These types of files are now much more common and useful when playing recorded music downloaded or ripped from music CDs. To play media files, open the file using a Uri of the filename. Various properties of the media file can be set including Balance, Volume, Position, Ratio and so on. Use the Play() function to begin playing and the Stop() function to stop playing.

public void playMusic_Click(object s, EventArgs e)
{
FileDialog fileDg = new OpenFileDialog();
fileDg.Filter = "MP3 files (*.mp3)|*.mp3|M4A files (*.m4a)|*.m4a|All files (*.*)|*.*";
fileDg.ShowDialog();
string file = fileDg.FileName;

if (file.Length != 0)
  {
    MediaPlayer mplay = new MediaPlayer();
    mplay.Open(new Uri(file));
    mplay.Play();
  }
}

14.4 Playing music via MIDI

It is also possible to make use of a MIDI interface to control and play music via electronic musical equipment. Unfortunately, there are no native .NET assemblies for this. Instead you can make use of the winmm.dll (MCI API DLL) via some definitions and functions. Full information is on this CodeGuru page.

long mciSendString( command, returnValue, returnLength, winHandle)
int midiOutGetNumDevs()
int midiOutGetDevCaps( deviceID, lpMidiOutCaps, dbMidiOutCaps)
int midiOutOpen( handle, proc, instance, flags )
int midiOutShortMsg( handle, message)
void MidiCallBack( handle, msg, instance, param1, param2 )