C # calls mciSendString to play audio files

Source: Internet
Author: User

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

The declaration of a function is as follows:

        Private Static extern Long mcisendstring (            string command ,      //MCI command string             // Buffer for storing feedback information            int returnsize,      // length of buffer            IntPtr hwndcallback  //  The handle of the callback window, usually null            );  

Second, the complete code is as follows, the details of which are annotated, pay special attention to the processing of the thread when playing:

usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Threading;namespacezhy.mci{/** Call API function mciSendString Play audio file * Mainly includes play by a specified number of times and loop play * Zhy * Time: 2015-7-21*/     Public classMCI {[DllImport ("Winmm.dll")]        Private Static extern LongmciSendString (stringCommand//MCI command String            stringReturnstring,//buffer for storing feedback information            intReturnsize,//the length of the bufferIntPtr Hwndcallback//handle to the callback window, typically null);//returns 0 if successful, otherwise the error code is returned.         /// <summary>        ///play by a specified number of times/// </summary>        /// <param name= "file" ></param>        Private voidPlaywait (stringfile) {            /** Open device_name type Device_type alias Device_alias turn on device * Device_name the device name to use, usually the file name             。             * Type Device_type device type, such as MPEGVideo or Waveaudio, can be omitted.             * Alias Device_alias device alias, specified to replace the device name in other commands. */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 play * If omitted from then start playing from the current track.             * If omitted to then play to the end. * If you specify wait, wait until the playback command is returned.             This indicates that wait will cause a thread to block until the playback is complete * If the repeat is indicated, it will be repeated repeatedly.             * If you specify both wait and repeat, the command will not return, and this thread will cause a blockage, which usually causes the program to lose its 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 voidPlayrepeat (stringfile) {mciSendString (string. Format ("Open {0} type MPEGVideo alias media", file),NULL,0, IntPtr.Zero); mciSendString ("Play Media repeat",NULL,0, IntPtr.Zero); }        Privatethread thread; /// <summary>        ///Play audio files/// </summary>        /// <param name= "file" >Audio file path</param>        /// <param name= "Times" >Play count, 0: Loop play greater than 0: play by a specified number of times</param>         Public voidPlay (stringFileintTimes ) {            //The thread is mainly used to solve the problem of thread blocking when the wait is specified during playback, which causes the interface to suspend animation.Thread =NewThread (() =                {                    if(Times = =0) {playrepeat (file); }                    Else if(Times >0)                    {                         for(inti =0; I < times; i++) {playwait (file);            }                    }                }); //thread must be single-threadedthread.            Setapartmentstate (ApartmentState.STA); Thread. IsBackground=true; Thread.        Start (); }        /// <summary>        ///to end a playing thread/// </summary>         Public voidExit () {if(Thread! =NULL)            {                Try{thread.                Abort (); }                Catch{} thread=NULL; }        }    }}

Third, call:

New MCI (). Play (" audio file path ", number of plays);

C # calls mciSendString to play audio files

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.