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("音頻文件路徑",播放次數);