Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Windows Forms
SoundPlayer Class
 How to: Play a Sound Embedded in a ...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Windows Forms Programming
How to: Play a Sound Embedded in a Resource from a Windows Form

Updated: November 2007

You can use the SoundPlayer class to play a sound from an embedded resource.

Visual Basic
Private Sub playSoundFromResource(ByVal sender As Object, _
ByVal e As EventArgs)
    Dim a As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
    Dim s As System.IO.Stream = a.GetManifestResourceStream("<AssemblyName>.chimes.wav")
    Dim player As SoundPlayer = New SoundPlayer(s)
    player.Play()
End Sub

C#
private void playSoundFromResource(object sender, EventArgs e)
{
    System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream s = a.GetManifestResourceStream("<AssemblyName>.chimes.wav");
    SoundPlayer player = new SoundPlayer(s);
    player.Play();
}

This example requires:

Importing the System.Media namespace.

Including the sound file as an embedded resource in your project.

Replacing "<AssemblyName>" with the name of the assembly in which the sound file is embedded. Do not include the ".dll" suffix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker