录音和播放声音 mciSendString


录音和播放声音 mciSendString

 

录音和播放声音 mciSendString  https://files.cnblogs.com/files/xe2011/mciSendString2020111183816.rar




mciSendString API

[DllImport("WinMm.dll",CharSet = CharSet.Auto,CallingConvention = CallingConvention.Cdecl)]
public static extern int mciSendString(string lpstrCommand,string lpstrReturnString,int uReturnLength,IntPtr hwndCallback);


定义录音文件路径

/* 注意
* 录音的保存的文件必须是.wav格式如果换成.mp3就出错
* 下面播放的方法文件格式即使是.mp3也是可以播放的
*/
string path = Guid.NewGuid() + ".wav";


录音

//开始录音
mciSendString("set wave bitpersample 8","",0,IntPtr.Zero);
 mciSendString("set wave samplespersec 20000","",0,IntPtr.Zero);
 mciSendString("set wave channels 2","",0,IntPtr.Zero);
 mciSendString("set wave format tag pcm","",0,IntPtr.Zero);
 mciSendString("open new type WAVEAudio alias movie","",0,IntPtr.Zero);//这个决定是wav文件
mciSendString("record movie","",0,IntPtr.Zero);




保存录音

 

//保存录音
mciSendString("stop movie","",0,IntPtr.Zero);
mciSendString($"save movie {path}","",0,IntPtr.Zero);//换成wav有效 换成mp3文件错误
mciSendString("close movie","",0,IntPtr.Zero);


播放

播放.wav文件,.mp3文件格式也可以播放

 //播放
string s = $"open \"{path}\" alias media";
mciSendString("close all",null,0,IntPtr.Zero);
mciSendString(s,null,0,IntPtr.Zero);
mciSendString("set media time format milliseconds",null,0,IntPtr.Zero);
mciSendString("play media",null,0,IntPtr.Zero);
mciSendString("play media notify",null,0,this.Handle);//必须加当前窗体句柄才能 触发 DefWndProc 的 条件 m.Msg == 0x3B9


判断播放是否结束

//判断指令是不是MM_MCINOTIFY0x3B9 //当歌曲播完 mciSendString()向系统发送的MM_MCINOTIFY指令


protected override void DefWndProc(ref Message m) {
    base.DefWndProc(ref m);
    if (m.Msg == 0x3B9) {
     Text = "播放已完成...";
     }
}
上一篇:Django 单表查询


下一篇:filter() 过滤函数