C#調用mciSendString播放音頻檔案

來源:互聯網
上載者:User

標籤:

mciSendString函數是一個WinAPI,主要用來向MCI(Media Control Interface)裝置發送字串命令。

一、函數的聲明如下:

        private static extern long mciSendString(            string command,      //MCI命令字串            string returnString, //存放反饋資訊的緩衝區            int returnSize,      //緩衝區的長度            IntPtr hwndCallback  //回調視窗的控制代碼,一般為NULL            );  

 

二、完整的代碼如下,其中的細節都有注釋說明,要特別注意播放時的線程處理:

using System;using System.Runtime.InteropServices;using System.Threading;namespace Zhy.MCI{    /*     * 調用API函數mciSendString播放音頻檔案     * 主要包括按指定次數播放以及迴圈播放     * Zhy     * 時間:2015-7-21     */    public class MCI    {        [DllImport("winmm.dll")]        private static extern long mciSendString(            string command,      //MCI命令字串            string returnString, //存放反饋資訊的緩衝區            int returnSize,      //緩衝區的長度            IntPtr hwndCallback  //回調視窗的控制代碼,一般為NULL            );                   //若成功則返回0,否則返回錯誤碼。        /// <summary>        /// 按指定次數播放        /// </summary>        /// <param name="file"></param>        private void PlayWait(string file)        {            /*             * open device_name type device_type alias device_alias  開啟裝置             * device_name     要使用的裝置名稱,通常是檔案名稱。             * type device_type  裝置類型,例如mpegvideo或waveaudio,可省略。             * alias device_alias 裝置別名,指定後可在其他命令中代替裝置名稱。             */            mciSendString(string.Format("open {0} type mpegvideo alias media", file), null, 0, IntPtr.Zero);            /*             * play device_alias from pos1 to pos2 wait repeat  開始裝置播放             * 若省略from則從當前磁軌開始播放。             * 若省略to則播放至結束。             * 若指明wait則等到播放完畢命令才返回。即指明wait會產生線程阻塞,直到播放完畢             * 若指明repeat則會不停的重複播放。             * 若同時指明wait和repeat則命令不會返回,本線程產生堵塞,通常會引起程式失去響應。             */            mciSendString("play media wait", null, 0, IntPtr.Zero);            /*             * close    關閉裝置             */            mciSendString("close media", null, 0, IntPtr.Zero);        }        /// <summary>        /// 迴圈播放        /// </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);        }        private Thread thread;        /// <summary>        /// 播放音頻檔案        /// </summary>        /// <param name="file">音頻檔案路徑</param>        /// <param name="times">播放次數,0:迴圈播放 大於0:按指定次數播放</param>        public void Play(string file, int times)        {            //用線程主要是為瞭解決在播放的時候指定wait時產生線程阻塞,從而導致介面假死的現象            thread = new Thread(() =>                {                    if (times == 0)                    {                        PlayRepeat(file);                    }                    else if (times > 0)                    {                        for (int i = 0; i < times; i++)                        {                            PlayWait(file);                        }                    }                });            //線程必須為單線程            thread.SetApartmentState(ApartmentState.STA);            thread.IsBackground = true;            thread.Start();        }        /// <summary>        /// 結束播放的線程        /// </summary>        public void Exit()        {            if (thread != null)            {                try                {                    thread.Abort();                }                catch { }                thread = null;            }        }    }}

 

三、調用:

new MCI().Play("音頻檔案路徑",播放次數);

 

 

C#調用mciSendString播放音頻檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.