使用C#调用win32 mciSendCommand 播放音乐
来源:互联网 发布:1002服务数据出现异常 编辑:程序博客网 时间:2024/04/30 03:43
以下是调用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; } }
0 0
- 使用C#调用win32 mciSendCommand 播放音乐
- 使用mciSendCommand循环播放音乐
- 使用mciSendCommand循环播放音乐
- 使用mciSendCommand循环播放音乐
- 音乐播放器 mciSendCommand
- 用mciSendCommand实现的音乐播放类
- C#播放音乐,调用程序
- C# winform使用系统API播放音乐(非调用控件)
- C#调用winmm播放音乐(.wav文件)
- C# 调用wav文件播放音乐
- win32 播放音乐&停止播放
- C#使用winmm播放mp3音乐
- VS2010 win32 音乐播放器
- 调用winmediaplay播放音乐
- C#调用mediaplayer控件打造音乐播放器
- C#调用WindowsMediaPlayer控件实现音乐播放(一)
- c# 播放音乐
- C# WM9播放音乐
- Fedora10下源码编译vlc-1.1.1 流程及注意事项
- PowerDesigner修改建库脚本的方法,尤其是外键生成方式
- Java GC
- 查看代码格式
- 使用JSON报的一个错误java.lang.ClassNotFoundException
- 使用C#调用win32 mciSendCommand 播放音乐
- PHP基础(1)
- Delphi 在任务栏隐藏程序图标
- GLSL
- JPA 注解
- verilog中的有符号数运算
- Codeforces Round #223 (Div. 2) B 哈希表
- sourceforge镜像
- nfs网络文件系统