本代码特点:不用DirectX ,对于C/S 、B/S都适用。
方法:
- //mciSendStrin.是用来播放多媒体文件的API指令,可以播放MPEG,AVI,WAV,MP3,等等,下面介绍一下它的使用方法:
- //第一个参数:要发送的命令字符串。字符串结构是:[命令][设备别名][命令参数].
- //第二个参数:返回信息的缓冲区,为一指定了大小的字符串变量.
- //第三个参数:缓冲区的大小,就是字符变量的长度.
- //第四个参数:回调方式,一般设为零
- //返回值:函数执行成功返回零,否则返回错误代码
- [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
- private static extern int mciSendString(
- string lpstrCommand,
- string lpstrReturnString,
- int uReturnLength,
- int hwndCallback);
- private static void mciSendString(String cmd)
- {
- mciSendString(cmd, "", 0, 0);
- }
- private static void StartRecord()
- {
- mciSendString("close movie");
- mciSendString("open new type WAVEAudio alias movie");
- mciSendString("record movie");
- }
- private static void StopRecord(string filename)
- {
- mciSendString("stop movie");
- mciSendString("save movie " + filename);
- mciSendString("close movie");
- }
用法举例:
- protected void btStart_Click(object sender, EventArgs e)
- {
- //开始录音
- StartRecord();
- }
- protected void btStop_Click(object sender, EventArgs e)
- {
- //停止录音
- StopRecord(@"C:\test.wav");
- }
- protected void btPlay_Click(object sender, EventArgs e)
- {
- //播放录音 也可以适用window系统带的TTS(Text To Speech)播放录音
- SoundPlayer sp = new SoundPlayer(@"c:\test.wav");
- sp.PlaySync();
- }