The first is to use DirectX
1. Install the DirectX SDK (with 9 DLL files ). Here we only use MicroSoft. DirectX. dll and Microsoft. Directx. DirectSound. dll.
2. The namespace of the imported DirectX DLL file:
Using Microsoft. DirectX;
Using Microsoft. DirectX. DirectSound;
3. Create a device
Device dv = new Device ();
4. Set CooperativeLevel. Because windows is a multitasking system, the device is not exclusive
SecondaryBuffer buf = new SecondaryBuffer (@ "snd.wav", dv );
5. Open a bufferSecondaryBuffer buf=new SecondaryBuffer(@"snd.wav",dv);
6. Now you can play the video. The first parameter indicates priority, and 0 indicates the lowest priority. The first parameter is the playback mode. Here it is loop playback.
Buf. Play (0, BufferPlayFlags. Looping );
The second method is to use Microsoft speech object Library
/// <Summary
/// Play the audio file
/// </Summary>
/// <Param name = "FileName"> full file name </param>
Public void PlaySound (string FileName)
{// COM component to be loaded: Microsoft speech object Library
If (! System. IO. File. Exists (FileName ))
{
Return;
}
SpeechLib. SpVoiceClass pp = new SpeechLib. SpVoiceClass ();
SpeechLib. SpFileStreamClass spFs = new SpeechLib. SpFileStreamClass ();
SpFs. Open (FileName, SpeechLib. SpeechStreamFileMode. SSFMOpenForRead, true );
SpeechLib. ISpeechBaseStream Istream = spFs as SpeechLib. ISpeechBaseStream;
Pp. SpeakStream (Istream, SpeechLib. SpeechVoiceSpeakFlags. SVSFIsFilename );
SpFs. Close ();
}
Third: Reference SoundPlayer
System. Media. SoundPlayer sndPlayer = new System. Media. SoundPlayer (Application. StartupPath + @ "/pm3.wav ");
SndPlayer. PlayLooping ();
4th types: use Windows Media Player
Create a C # Windows Form Project (Windows application) and define two menu buttons (menuItem1, menuItem2 ).
Select "Custom Toolbox (Add/Remove toolbox items)" in "Tools" in the menu. In the custom Toolbox window, click to expand the "COM component" item, select "Window Media Player. After confirmation, the "Windows Media Player" item will appear in the "toolbox", and then drag it to the Form to resize it, the system automatically adds a reference to this dll in "Reference". AxMediaPlayer is the Namespace and class we use.
Set some attributes of this control in the property bar. For convenience, here I set AutoStart to true (in fact, the default value is true), as long as FileName is set (the file is opened ), the file is automatically played. The complete code is as follows:
Private void menuItem1_Click (object sender, System. EventArgs e)
{
OpenFileDialog ofDialog = new OpenFileDialog ();
OfDialog. AddExtension = true;
OfDialog. CheckFileExists = true;
OfDialog. CheckPathExists = true;
// The next sentence must be in single line
OfDialog. Filter = "VCD file (*. dat) | *. dat | Audio file (*. avi) | *. avi
| WAV file (*. wav) | *. wav | MP3 file (*. mp3) | *. mp3 | all files (*. *) | *.*";
OfDialog. DefaultExt = "*. mp3 ";
If (ofDialog. ShowDialog () = DialogResult. OK)
{
// Version 2003: this. axMediaPlayer1.FileName = ofDialog. FileName;
This. axMediaPlayer1.URL = ofDialog. FileName; // usage 2005
}
}
Here we use a Microsoft player. You can also try the Winamp control. If you only need to play the sound without displaying it, you only need to set the Visible attribute of the AxMediaPlayer to false.