C # learning notes-simple sound playback

Source: Internet
Author: User

Now I am working on a simple C # software. In order to beautify and multimedia things, I need to play music when the form is started. I found this on msdn and recorded it for later use!

Source code:

Using system;
Using system. runtime. interopservices;

Namespace GIS
{
/// <Summary>
/// Summary of cmedia.
/// </Summary>
Internal class csoundplay
{
Public csoundplay ()
{
//
// Todo: add the constructor logic here
//
}

[Flags]
Public Enum playsoundflags: int
{
Snd_sync = 0x0000,/* play synchronously (default )*/
Snd_async = 0x0001,/* Play asynchronously */
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. dll")]
Public static extern bool playsound (string szsound, intptr hmod, playsoundflags flags );

}

Public class sound
{
Public static void play (string strfilename)
{
Csoundplay. playsound (strfilename, intptr. Zero, csoundplay. playsoundflags. snd_filename | csoundplay. playsoundflags. snd_async );
}
}

}

 

1. Add the above Code to your project;

2. Call: Sound. Play (soundfilepathandname );

Note: This method must ensure that the winmm. dll file is available on your machine. What is the role of this file! You can query it online!

 

 

 


Usage:

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.