There are three main ways to play sound in C #:
1. Use the API functions.
2. Use the SoundPlayer class to play.
3. Use DirectX for playback.
First, use the API function to play.
Functions for sound processing are encapsulated in the Winmm.dll file in the Windows operating system. In C # We can use the API functions of this side to play the sound in the way of platform invoke.
The PlaySound, sndPlaySound, mcisendstring 3 API functions are used in the class below to play the sound.
The mcisendstring can also play MP3-formatted sounds.
Using system;using system.collections.generic;using system.linq;using system.text;using System.runtime.interopservices;namespace huaxu{public Static Class wavplayer{[DllImport ("Winmm.dll", SetLastError = t Rue)] static extern bool PlaySound (string pszsound, UIntPtr hmod, uint fdwsound); [DllImport ("Winmm.dll", SetLastError = True)] static extern long mcisendstring (string strcommand, StringBuilder Strreturn,int ireturnlength, IntPtr hwndcallback); [DllImport ("Winmm.dll")] private static extern long sndPlaySound (string lpszsoundname, long uflags); [Flags] public enum Soundflags {//<summary>play synchronously (default) </summary> snd_s YNC = 0x0000,///<summary>play asynchronously</summary> Snd_async = 0x0001,///<summ Ary>silence (!default) if sound not found</summary> Snd_nodefault = 0x0002,///<summary>pszs Ound points to a memory file</summary> SNd_memory = 0x0004,///<summary>loop the sound until next sndplaysound</summary> Snd_loop = 0x0 008,///<summary>don ' t stop any currently playing sound</summary> Snd_nostop = 0x0010,/ <summary>stop Playing wave</summary> snd_purge = 0x40,///<summary>don ' t wait if the D River is busy</summary> snd_nowait = 0x00002000,//<summary>name is a registry Alias</summa Ry> Snd_alias = 0x00010000,//<summary>alias is a predefined id</summary> Snd_alias_ ID = 0x00110000,//<summary>name is file name</summary> snd_filename = 0x00020000,/// <summary>name is resource name or atom</summary> Snd_resource = 0x00040004} public static void P Lay (String strFileName) {PlaySound (strFileName, Uintptr.zero, (UINT) (Soundflags.snd_filename | Soundflags.snd_sync | SoundflAgs. Snd_nostop)); } public static void Mciplay (String strfilename) {string playcommand = "Open" + strFileName + "type Waveau Dio alias Mywav "; mciSendString (Playcommand, NULL, 0, IntPtr.Zero); mciSendString ("Play mywav", NULL, 0, IntPtr.Zero); public static void Sndplay (String strfilename) {sndPlaySound (strFileName, (long) soundflags.snd_sync); }}}
Where the Soundflags enumeration defines the various ways to play.
Second, use SoundPlayer to play sound.
The class SoundPlayer under the System.Media namespace allows us to play WAV waveform sound files conveniently. The SoundPlayer class is actually the encapsulation of the API functions in the Winmm.dll file.
The use of the SoundPlayer class is simple. As follows:
SoundPlayer player = new SoundPlayer (); Player. soundlocation = "Music file name"; Player. Load (); Player. Play ();
Where the play method is an async method, it plays in another thread. If we sometimes need to wait for the sound to play, then do the next step. That is, sound playback needs to block the current thread. You can use Playsync ()
Method.
The disadvantage of the SoundPlayer class: only WAV files can be played, in WinXP the file is larger or higher than the bit rate, Playsync synchronous playback will have the problem of incomplete playback.
This problem arises due to the winmm.dll version problem. The version of Winmm.dll under XP is 5. Under the Win7 is the 6. There is no problem under the Win7. If you want to resolve the
The problem of incomplete playback under XP. You can use the recorder under XP to open the sound file, save the sound file as a 7kbit/s bit rate format, but the sound effect is very poor.
Third, use DirectX for playback.
The first thing to install is the DirectX SDK. After installation, there are DLL files that can be used under. NET in the C:\Windows\Microsoft.NET\DirectX for Managed code\1.0.2902.0 directory.
First reference Add Reference Microsoft.DirectX.AudioVideoPlayback
Using Microsoft.DirectX.AudioVideoPlayback;
Then instantiate the object of the Audo class, and you can play a music file that includes the MP3 format.
Audio audio = new Audio ("Hello.mp3"); Audio.play ();
Reprint http://www.cnblogs.com/chenjiahong/articles/2716552.html
C # plays a sound file