C # There are four methods to play background music:
1. Play the system event sound
2. Use System. Media. soundplayer to play wav
3. Use the MCI command string multimedia device program interface to play MP3, Avi, etc.
4. Use the COM component of axwindowsmediaplayer to play the video.
1. Play the 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
System. Media. soundplayer sp = new soundplayer (); Sp. soundlocation = @ "D:/10sec.wav "; Sp. playlooping (); |
3. Use the MCI command string multimedia device program interface to play MP3, Avi, etc.
Using system. runtime. interopservices; Public static uint snd_async = 0x0001; Public static uint snd_filename = 0x00020000; [Dllimport ("winmm. dll")] Public static extern uint mcisendstring (string lpstrcommand, String lpstrreturnstring, uint ureturnlength, uint hwndcallback ); Public void play () { Mcisendstring (@ "Close temp_alias", null, 0, 0 ); Mcisendstring (@ "open" "E:/music/Qinghua porcelain" "alias temp_alias", null, 0, 0 ); Mcisendstring ("play temp_alias repeat", null, 0, 0 ); } |
For detailed parameter descriptions for mcisendstring, see msdn or http://blog.csdn.net/psongchao/archive/2007/01/19/1487788.aspx
4. Use the COM component of axwindowsmediaplayer to play the video.
A. Load COM components: toolbox-> choose items-> COM components-> Windows Media Player, for example:
B. Drag the Windows Media Player control to the winform, set the URL attribute in axwindowsmediaplayer1 to the file path of MP3 or AVI, and run F5.
How do I use a media file in the Windows Media Player loop playback list?
Suppose we have a playlist. The following code can enable automatic loop playback.
Private void axwindowsmediaplayer=playstatechange (Object sender, axwmplib. _ wmpocxevents_playstatechangeevent E) { If (axwindowsmediaplayer1.playstate = wmplib. wmpplaystate. wmppsmediaended) { Thread thread = new thread (New threadstart (playthread )); Thread. Start (); } } Private void playthread () { Axwindowsmediaplayer1.url = @ "E:/music/someone. Avi "; Axwindowsmediaplayer1.ctlcontrols. Play (); } |
Both the MCI command string and Windows Media Player have rich functional interfaces. You cannot describe them one by one here. For details, refer to the description in msdn.