C # Four Methods for playing sound

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.