C # Four common methods for playing background music

Source: Internet
Author: User

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.

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.