C # Call winmm. DLL to play audio _ created in China

Source: Internet
Author: User

C # Call the C # sample code below winmm. DLL to play the audio file # demonstrate how to use the platform call service on Windows platform to play the waveform sound file.
Example
This code example uses dllimport to import the playsound method entry point of winmm. dll as form1 playsound. This example has a simple windows form with a button. Click this button to open the standard Windows openfiledialog dialog box to open the file to be played. After the waveform file is selected, the playsound () method of the winmm. dll assembly method is used to play the file. For more information about the playsound method of winmm. dll, see using playsound to play Waveform-audio files (use playsound to play waveform audio files ). Browse and select a file with the. wav extension, and click "open" to use the platform call to play the waveform file. The complete path of the selected file is displayed in the text box.
The "Open File" dialog box is filtered to display only files with. wav extensions. The filter is set:
C # copy to clipboard
Dialog1.filter = "WAV Files (*. wav) | *. wav ";

C # copy to clipboard
Using system. Windows. forms;

Namespace winsound
{
Public partial class form1: Form
{
Private textbox textbox1;
Private button button1;

Public form1 () // Constructor
{
Initializecomponent ();
}

[System. runtime. interopservices. dllimport ("winmm. dll", entrypoint = "playsound", setlasterror = true)]
Private Static extern bool playsound (string szsound, system. intptr hmod, playsoundflags flags );

[System. Flags]
Public Enum playsoundflags: int
{
Snd_sync = zero x 0000,
Snd_async = 0x0001,
Snd_nodefault = 0x0002,
Snd_loop = 0x0008,
Snd_nostop = 0x0010,
Snd_nowait = 0x00002000,
Snd_filename = 0x00020000,
Snd_resource = 0x00040004
}

Private void button#click (Object sender, system. eventargs E)
{
Openfiledialog dialog1 = new openfiledialog ();

Dialog1.title = "browse to find sound file to play ";
Dialog1.initialdirectory = @ "C :/";
Dialog1.filter = "WAV Files (*. wav) | *. wav ";
Dialog1.filterindex = 2;
Dialog1.restoredirectory = true;

If (dialog1.showdialog () = dialogresult. OK)
{
Textbox1.text = dialog1.filename;
Playsound (dialog1.filename, new system. intptr (), playsoundflags. snd_sync );
}
}
}
}

3. Compile the code
3. Compile the code
Create a new C # windows application project in Visual Studio and name it winsound.
Copy the above Code and paste it into the form1.cs file to overwrite the original content.
Copy the following code and paste it behind the existing code in the initializecomponent () method of the form1.designer. CS file.
C # copy to clipboard
This. button1 = new system. Windows. Forms. Button ();
This. textbox1 = new system. Windows. Forms. Textbox ();
This. suspendlayout ();
//
// Button1
//
This. button1.location = new system. Drawing. Point (192, 40 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (88, 24 );
This. button1.tabindex = 0;
This. button1.text = "Browse ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (8, 40 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (168, 20 );
This. textbox1.tabindex = 1;
This. textbox1.text = "file path ";
//
// Form1
//
This. autoscaledimensions = new system. Drawing. sizef (5, 13 );
This. clientsize = new system. Drawing. Size (292,266 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. button1 );
This. Name = "form1 ";
This. Text = "platform invoke winsound C #";
This. resumelayout (false );
This. initialize mlayout ();

4. Compile and run the code.

This article is transferred from
Http://hi.baidu.com/%D6%D0%B9%FA%B4%B4%D4%EC/blog/item/b8089a50335bc462853524ad.html

Related Article

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.