Play music with winmm-chengking-blog Garden

Source: Internet
Author: User

Play music with winmm
Posted on chengking reading (110) Comments (0) edit the category of the favorites:. Net Method in using DLL, C # column
(1). Description
For a music playing class, you can use copy directly.
It can be used for self-compiled games and other places where music needs to be played. You only need to give it a file name.
OK. For details, refer to the following code.
(2). Steps
Using system;
// Using system. drawing;
Using system. collections;
Using system. componentmodel;
// Using system. Windows. forms;
// Using system. Data;
Using system. runtime. interopservices;
Namespace chess game
{
///

/// Used to play music
///

Internal class helpers
{
[Flags]
Public Enum playsoundflags: int
{
Snd_sync = 0x0000,/* play synchronously (default) * // synchronize
Snd_async = 0x0001,/* Play asynchronously * // asynchronous
Snd_nodefault = 0x0002,/* silence (! Default) if sound not found */
Snd_memory = 0x0004,/* pszsound points to a memory file */
Snd_loop = 0x0008,/* loop the sound until next sndplaysound */
Snd_nostop = 0x0010,/* Don ???? T stop any currently playing sound */
Snd_nowait = 0x00002000,/* Don ???? T wait if the driver is busy */
Snd_alias = 0x00010000,/* name is a registry alias */
Snd_alias_id = 0x00110000,/* alias is a predefined ID */
Snd_filename = 0x00020000,/* name is file name */
Snd_resource = 0x00040004/* Name Is Resource Name or atom */
}
[Dllimport ("winmm")]
Public static extern bool playsound (string szsound, intptr hmod, playsoundflags flags );
}
Public class sound
{
Public static void play (string strfilename)
{
// Helpers. playsound (strfilename, intptr. Zero, helpers. playsoundflags. snd_filename | helpers. playsoundflags. snd_async );
Switch (strfilename)
{
Case "start": strfilename = @ ".../sound/start. wav"; break;
Case "back": strfilename = @ ".../sound/back. wav"; break;
Case "fall": strfilename = @ ".../../sound/fall. wav"; break;
Case "Huiqi": strfilename = @ ".../../sound/Huiqi. wav"; break;
Case "huiqiend": strfilename = @ ".../../sound/huiqiend. wav"; break;
Case "Jiangjun": strfilename = @ ".../../sound/Jiangjun. wav"; break;
Case "kill": strfilename = @ ".../../sound/kill. wav"; break;
Case "win": strfilename = @ ".../sound/win. wav"; break;
Case "move": strfilename = @ ".../sound/move. wav"; break;
Case "hold": strfilename = @ ".../sound/hold. wav"; break;
Case "no": strfilename = @ ".../sound/No. wav"; break;
Case "popup": strfilename = @ ".../../sound/Popup. wav"; break;
Case "mayfall": strfilename = @ ".../../sound/mayfall. wav"; break;
}
Helpers. playsound (strfilename, intptr. Zero, helpers. playsoundflags. snd_filename | helpers. playsoundflags. snd_async); // call the DLL method to play music.
}
}
}
(3). Use
1. Add components (InterOP. wmplib). This class library comes with vs. NET and is not added by default.
Method: expand the project folder, right-click reference, add reference, go to the COM component bar, and find add to project.
2. Create a New *. CS file, copy the code, and overwrite it all, and modify the playback file name in the example above.
This is the statement: strfilename = @ ".../../sound/mayfall. wav"; // This music file must exist; otherwise, it cannot be played.
3. If this method is called in a different namespace than this class, open the namespace of the class where it is used.
Method: for example, the namespace of this example class should be written as follows: Using Intelligent chess game;
Finally, you can use it. The usage is extremely simple. You can directly call this method:
Sound. Play ("move"); // The parameter customization. It has nothing to do with the underlying method. You can see the swith statement in the above class.

This article is transferred from
Http://www.cnblogs.com/chengking/articles/277275.html

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.