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