Play an audio resource in C#

This example plays several audio resources that were added to the project. For information on adding resources, see Use resources to hold images, text, and other data, and load it at run time in C#.

The following code shows how to play an audio resource.

private void btnMeow_Click(object sender, EventArgs e)
{
using (SoundPlayer player = new SoundPlayer(
Properties.Resources.Meow))
{
player.Play();
}
}

The code creates a SoundPlayer, passing its constructor the audio resource. The SoundPlayer class is in the System.Media namespace so the program includes a using statement to make creating the SoundPlayer easier.

When it has finished using the SoundPlayer, the program should call its Dispose method so the code creates it in a using statement to make that easier.

After creating the object, the code simply calls its Play method. The SoundPlayer does the rest.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.