使用C#调用win32 mciSendCommand 播放音乐
public class PlaySound{////// 调用系统API//////////////////[DllImport("winmm.dll", EntryPoint = "mciSendString"
·
以下是调用win32播放音乐的类:
public class PlaySound
{
/// <summary>
/// 调用系统API
/// </summary>
/// <param name="lpszCommand"></param>
/// <param name="lpszReturnString"></param>
/// <param name="cchReturn"></param>
/// <param name="hwndCallback"></param>
/// <returns></returns>
[DllImport("winmm.dll", EntryPoint = "mciSendString", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr mciSendString(String lpszCommand, String lpszReturnString, int cchReturn, int hwndCallback);
[DllImport("winmm.dll", EntryPoint = "mciGetErrorString", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool mciGetErrorString(int fdwError,ref String lpszErrorText, int UINT);
private static String fileName;
public static void LoadFile(string filePath)
{
isPlaying = false;
fileName = filePath;
}
public static void Play()
{
PlaySound.mciSendString(String.Format("open {0} alias mysong", fileName), "", 0, 0);
PlaySound.mciSendString("play mysong repeat", "", 0, 0);
isPlaying = true;
}
public static bool isPlaying = false;
public static void Stop()
{
PlaySound.mciSendString("stop mysong", "", 0, 0);
isPlaying = false;
}
public static void Pause()
{
isPlaying = false;
PlaySound.mciSendString("pause mysong", "", 0, 0);
}
public static string GetErrMsg()
{
string s=string.Empty;
mciGetErrorString(1, ref s, 0);
return s;
}
}
调用如下:
class Program
{
[STAThread]
static void Main(string[] args)
{
PlaySound.LoadFile("等一分钟.mp3");
PlaySound.Play();
while (true) ;
}
}
如何在一个应用程序中,创建多个线程,每个线程都同时播放:
class Program
{
[STAThread]
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Thread th = new Thread(new ThreadStart(PlaySong));
th.SetApartmentState(ApartmentState.STA);
th.Start();
Thread.Sleep(1000 * 5);
}
while (true) ;
}
static void PlaySong()
{
PlaySound pl = new PlaySound();
pl.LoadFile("等一分钟.mp3");
pl.Play();
while (true) ;
}
}
public class PlaySound
{
public bool isPlaying = false;
/// <summary>
/// 调用系统API
/// </summary>
/// <param name="lpszCommand"></param>
/// <param name="lpszReturnString"></param>
/// <param name="cchReturn"></param>
/// <param name="hwndCallback"></param>
/// <returns></returns>
[DllImport("winmm.dll", EntryPoint = "mciSendString", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr mciSendString(String lpszCommand, String lpszReturnString, int cchReturn, int hwndCallback);
[DllImport("winmm.dll", EntryPoint = "mciGetErrorString", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool mciGetErrorString(int fdwError, ref String lpszErrorText, int UINT);
private String fileName;
public void LoadFile(string filePath)
{
isPlaying = false;
fileName = filePath;
}
public void Play()
{
mciSendString(String.Format("open {0} alias mysong", fileName), "", 0, 0);
mciSendString("play mysong repeat", "", 0, 0);
isPlaying = true;
}
public void Stop()
{
mciSendString("stop mysong", "", 0, 0);
isPlaying = false;
}
public void Pause()
{
isPlaying = false;
mciSendString("pause mysong", "", 0, 0);
}
public string GetErrMsg()
{
string s = string.Empty;
mciGetErrorString(1, ref s, 0);
return s;
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)