C # uses Windows to bring Winmm.dll to play audio

Source: Internet
Author: User

To help colleagues complete a C # application today, the user needs to make a specified sound when the click is complete. We use Windows Winmm.dll, so that we do not have to look for libraries everywhere, the system to play audio features to complete the development.

The following is the complete core code:

1 usingSystem.Runtime.InteropServices;//DllImport namespaces
2[DllImport ("Winmm.dll")]privatestaticexternlong mciSendString (stringLpstrcommand,stringbuilder lpstrreturnstring,intUreturnlength,intHwndcallback);//C # Methods for calling Windows native DLLs
3 Const stringAudiofile ="Ok.mp3"; //Audio file path 4Privatevoid Btnplay_click (ObjectSender,eventargs e) {5    stringcommandstring="Open"+"\""+ audiofile+"\""+"type MPEGVideo alias Mp3file";//Note that the mp3file behind alias is the alias you take, of course, the English meaning is the alias. So you can take it casually, but be sure to have the same name as in the following command. Note "\" ", you can not, but it is best to keep, or accidentally forget to leave the" type "before the space, the entire command is not delimited, in fact, the entire command can also be used without the" type MPEGVideo ", but do not forget to add spaces before alias (if there is \" can not be used). 6mciSendString (CommandString,NULL,0,0);7commandstring="set mp3file time format Ms";8mciSendString (CommandString,NULL,0,0);9CommandString ="seek mp3file to 0";//0 That is the beginning of the audio, of course, the first time is originally in the beginning, but if you repeat the action, do not place the playback position in 0-bit, then the first time to play will not be able to broadcast the sound, because the system has been playing files to the end. TenmciSendString (CommandString,NULL,0,0); Onecommandstring="Play Mp3file"; AmciSendString (CommandString,NULL,0,0); -}//

In fact, it is simple to call the system audio playback function by command. The above code can play WAV and other formats, just need to remove the "type MPEGVideo", this paragraph will be OK.

C # uses Windows to bring Winmm.dll to play audio

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.