C # Call mciSendString to play the audio file,

Source: Internet
Author: User

C # Call mciSendString to play the audio file,

The mciSendString function is a WinAPI used to send string commands to the Media Control Interface (MCI) device.

I. function declaration:

Private static extern long mciSendString (string command, // MCI command string returnString, // buffer that stores feedback int returnSize, // The length of the buffer IntPtr hwndCallback // the handle of the callback window, usually NULL );

 

II. The complete code is as follows. The details are annotated and described. Pay special attention to the thread processing during playback:

Using System; using System. runtime. interopServices; using System. threading; namespace Zhy. MCI {/** call the API function mciSendString to play audio files * mainly includes playing audio files by a specified number of times and loop playback * Author: Zhy * Time: 2015-7-21 */public class MCI {[DllImport ("winmm. dll ")] private static extern long mciSendString (string command, // MCI command string returnString, // buffer that stores feedback int returnSize, // The length of the buffer IntPtr hwndCallback // the handle of the callback window, usually NULL); // If successful, 0 is returned; otherwise, an error code is returned.. /// <Summary> /// play by the specified number of times /// </summary> /// <param name = "file"> </param> private void PlayWait (string file) {/** open device_name type device_type alias device_alias open the device * The name of the device to be used for device_name, usually the file name. * The type device_type device type, such as mpegvideo or waveaudio, can be omitted. * Alias device_alias: Specifies the device alias. You can use other commands to replace the device name. */MciSendString (string. format ("open {0} type mpegvideo alias media", file), null, 0, IntPtr. zero);/** play device_alias from pos1 to pos2 wait repeat start device playback * if it is omitted from, start playing from the current track. * If to is omitted, the playback ends. * If wait is specified, it will not be returned until the Playback command is completed. That is, it indicates that wait will cause thread blocking until the playback is complete * If repeat is specified, it will not stop playing again. * If both wait and repeat are specified, the command will not return. This thread is congested and will usually cause program to lose response. */MciSendString ("play media wait", null, 0, IntPtr. zero);/** close device */mciSendString ("close media", null, 0, IntPtr. zero );} /// <summary> /// loop playback /// </summary> /// <param name = "file"> </param> private void PlayRepeat (string file) {mciSendString (string. format ("open {0} type mpegvideo alias media", file), null, 0, IntPtr. zero); mciSendString ("play media repeat", null, 0, IntPtr. zero);} pr Ivate Thread thread; /// <summary> /// play the audio file /// </summary> /// <param name = "file"> audio file path </param> /// <param name = "times"> playback times, 0: loop playback greater than 0: playback by a specified number of times </param> public void Play (string file, int times) {// The thread is mainly used to solve the problem of Thread blocking when the wait is specified during playback, leading to false UI thread = new Thread (() ==>{ if (times = 0) {PlayRepeat (file);} else if (times> 0) {for (int I = 0; I <times; I ++) {PlayWait (file) ;}}}); // The thread must be a single thread t Hread. setApartmentState (ApartmentState. STA); thread. isBackground = true; thread. start () ;}/// <summary> /// end the playing thread // </summary> public void Exit () {if (thread! = Null) {try {thread. Abort () ;}catch {} thread = null ;}}}}

 

Iii. call:

New MCI (). Play ("audio file path", playback times );

 

 

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.