Author: salysystemic
Address: http://www.codeproject.com/KB/audio-video/PlayWavFiles.aspx
Content summary
This articleArticleDemonstrate how to embed WAV audio resource files into ApplicationsProgramAnd play it through the system. media class library.
Introduction
This article demonstrates how to embed a WAV audio resource file into an application and play it through the system. media class library.
With the help of the system. media class library, we do not need to reference winmm. DLL to play audio resources, andCodeIt is easier to implement.
Audio files are directly embedded into the program, which makes it unnecessary to add additional audio files to the installation package during program deployment. In addition, this method also ruled out the possibility that the file does not exist or is deleted,
It will cause the program to fail to find the desired sound file when it needs resources.
Figure 1 example application form
Start
InSource codeContains only one solution named playwavfiles. As shown in Project 2, this project contains a reference to the default win forms, and resources are directly embedded into application resources without distributing resources during installation.
Figure 2 solution Browser
This example is written in C # and compiled in the integrated development environment of Visual Studio 2005. You can open it with IDE to check the code.
Add audio file resources
To add a resource file to the solution, open the resources. resx file in the solution browser. After opening, you can see a drop-down box, 3, which is used to select the type of resources added to the project. Select the Audio Resource and click Add Resource.
Figure 3 Add a sound file resource
After you click Add resource, a file browsing dialog box is displayed. You can find the sound file and add it to the application resources.
Figure 4 view sound resources
After a sound file is added to an application resource file, select each sound resource in sequence and set their persistence (persistence) attribute to embedded in. resx (embedded in the resx file)
Figure 5 set the persistence attribute of the added audio file
So far, the sound file has been added to the project, and the properties have been set, you can use the sound resources in the project.
Code: Main Window
In this example, the application contains only one form (frmmain) file, which contains all the necessary code for playing embedded audio files. In addition to some necessary class libraries for import, you also need to add a reference to system. Media.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Media;
Namespace playwavfiles
{
Public partial class frmmain: Form
{
Public frmmain ()
{
Initializecomponent ();
}
All the necessary code is included in the click events of the three buttons; the exit button is used to exit the application:
Private void btnexit_click (Object sender, eventargs E)
{
Application. Exit ();
}
The click event of this button is used to play the embedded WAV file, which is played only once.
Private void btndemow.click (Object sender, eventargs E)
{
Try
{
Soundplayer sndplayr = new
Soundplayer (playwavfiles. properties. Resources. buzzingbee );
Sndplayr. Play ();
}
Catch (exception ex)
{
MessageBox. Show (ex. Message + ":" + ex. stacktrace. tostring (),
"Error ");
}
}
In the Click Event code of the button, create an instance of the soundplayer class and set its constructor parameter to the Application resource to be played. When the resource is loaded to the soundplayer instance, call the play method to play the sound.
The click event of this button demonstrates how to play embedded audio files cyclically:
Private void btndemo2_click (Object sender, eventargs E)
{
Try
{
Soundplayer sndplayr = new
Soundplayer (playwavfiles. properties. Resources. loopymusic );
If (btndemo2.text = "demo WAV 2 ")
{
Sndplayr. playlooping ();
Btndemo2.text = "stop ";
}
Else
{
Sndplayr. Stop ();
Btndemo2.text = "demo WAV 2 ";
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message + ":" + ex. stacktrace. tostring (),
"Error ");
}
}
The code in this example is similar to that in the previous example. The difference is that it calls the playlooping method instead of the play method. When this method is called, the loaded WAV file will be played cyclically until you call it to play another WAV file or call the stop method. To support this feature, when loop playback starts, the text of the button should be changed to stop. When you click the stop text button, the stop method is called, in addition, the text of the button is restored to the initial text, and the subsequent clicks will play the audio file, and then stop. The button will be updated alternately.
Summary
This example shows how to embed sound resources in Application resource files, how to retrieve sound files, and how to use the system. media class library for playback. Similar functions can also be obtained by using winmm. dll. Of course, this method only requires less Code and is safer from the deployment perspective.