C # plays background music typically in four ways:
1. Play System Event Sound
2. Use System.Media.SoundPlayer to play WAV------------------------is just for waveform music
3. Using MCI Command string Multimedia Device program interface play Mp3,avi, etc.
4. Use the COM component of AxWindowsMediaPlayer to play
1. Play System Event Sound
System.Media.SystemSounds.Asterisk.Play (); System.Media.SystemSounds.Beep.Play (); System.Media.SystemSounds.Exclamation.Play (); System.Media.SystemSounds.Hand.Play (); System.Media.SystemSounds.Question.Play ();
2. Use System.Media.SoundPlayer to play WAV
New@ "d:/10sec.wav"; Sp. Playlooping ();
3. Using MCI Command string Multimedia Device program interface play Mp3,avi, etc.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Runtime.InteropServices;namespacemywenxintool{ Public classMusicplay { Public Static UINTSnd_async =0x0001; Public Static UINTSnd_filename =0x00020000; [DllImport ("Winmm.dll")] Public Static extern UINTmciSendString (stringLpstrcommand,stringLpstrreturnstring,UINTUreturnlength,UINThwndcallback); Public Static voidPlaynmusinc (stringpath) {mciSendString (@"Close Temp_alias",NULL,0,0); mciSendString (@"Open """+path+@""Alias Temp_alias",NULL,0,0); mciSendString ("Play Temp_alias Repeat",NULL,0,0); } /// <summary> ///Play music files (repeat)/// </summary> /// <param name= "P_filename" >Music file name</param> Public Static voidPlaymusic_repeat (stringp_filename) { Try{mcisendstring (@"Close Temp_music"," ",0,0); mciSendString (@"Open"+ P_filename +"alias Temp_music"," ",0,0); mciSendString (@"Play Temp_music Repeat"," ",0,0); } Catch { } } /// <summary> ///Play music files/// </summary> /// <param name= "P_filename" >Music file name</param> Public Static voidPlaymusic (stringp_filename) { Try{mcisendstring (@"Close Temp_music"," ",0,0); //mcisendstring (@ "OPEN" + P_filename + "Alias Temp_music", "", 0, 0);mciSendString (@"Open """+ P_filename +@""Alias Temp_music",NULL,0,0); mciSendString (@"Play Temp_music"," ",0,0); } Catch { } } /// <summary> ///Stop the current music playback/// </summary> /// <param name= "P_filename" >Music file name</param> Public Static voidStopmusic (stringp_filename) { Try{mcisendstring (@"Close"+ P_filename," ",0,0); } Catch { } } }}
For detailed parameter descriptions of mcisendstring, see MSDN, or Http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
4. Use the COM component of AxWindowsMediaPlayer to play
A. Load COM components: Add reference-->com Components--Windows Media player such as:
B. Drag and drop the Windows Media Player control into the WinForm form, and set the URL property in AxWindowsMediaPlayer1 to MP3 or Avi's file path, F5 run.
How do I use Windows Media Player to loop media files in a playlist?
Assuming we have a playlist, the following code can be automatically looped
New= Txpath. Text.trim (); Mr. Play ();
MCI Command string and Windows Media player have a very rich feature interface, which cannot be described here, and can be found in the specific description on MSDN.
4 ways to play Music in C # Window form