C# 录音控制

2017-06-26 19:22:55来源:CSDN作者:qq_36011182人点击

最近做的项目需要用到C#的录音功能,在网上找了不少资源,大部分都是说使用DX来录音,下载下来demo大部分都是缺少东西,也确实是没有弄明白。下面介绍一个简单的录音方式,这也是翻阅了大神的博客了解到的,在这里进行了一些本地化的扩展。

在这个操作类下公开了两个方法和两个事件,方法分别是开始录音和结束录音,在结束录音时,可以传递一个参数作为取消录音的控制,传入true后将不会保存出文件。

这两个事件,一个时实时反馈进度的事件,可以使你实时控制进度条的显示,一个是录制完成的事件,即当你控制录制结束时,就会产生该事件,这个时间返回了录制的时间(采用进1法),录制的语音文件地址,是否是被取消的状态。事件的参数类这里没有给出,可以根据传递的参数自行添加或修改。


/// <summary>/// 语音录制控制类/// </summary>public class RecordingAudio{/// <summary>/// 录制完成事件,该事件只会在UI线程上执行/// </summary>public event EventHandler<RecordingFinishEventArgs> OnRecordingFinish;/// <summary>/// 录制进度事件,该事件只会在UI线程上执行/// </summary>public event EventHandler<RecordingProgressEventArgs> OnRecordingProgress;private int duration = 0;private Timer timer;/// <summary>/// 接收的主线程控件,使跨线程的操作回归到主线程上产生事件/// </summary>private Control control;/// <summary>/// 锁/// </summary>private object _obj = new object();public RecordingAudio(Control control){this.control = control;this.timer = new Timer(200);this.timer.Elapsed += timer_Tick;}/// <summary>/// 获取当前是否是正在录制状态/// </summary>public bool IsRecordingAudio { get; private set; }[DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]public static extern int mciSendString(string lpstrCommand,string lpstrReturnString,int uReturnLength,int hwndCallback);/// <summary>/// 更新进度/// </summary>/// <param name="p">进度值</param>private void UpdateProgress(double p){if (OnRecordingProgress != null){this.control.Dispatcher.Invoke(new Action(() =>{OnRecordingProgress(this, new RecordingProgressEventArgs(p));}));}}/// <summary>/// 录制完成/// </summary>private void RecordingFinish(int t, string path, bool isCancle){if (OnRecordingFinish != null){this.control.Dispatcher.Invoke(new Action(() =>{OnRecordingFinish(this, new RecordingFinishEventArgs(t, path, isCancle));}));}}/// <summary>/// 开始录制/// </summary>public void Start(){lock (_obj){if (IsRecordingAudio) return;mciSendString("set wave bitpersample 8", "", 0, 0);mciSendString("set wave samplespersec 20000", "", 0, 0);mciSendString("set wave channels 2", "", 0, 0);mciSendString("set wave format tag pcm", "", 0, 0);mciSendString("open new type WAVEAudio alias movie", "", 0, 0);mciSendString("record movie", "", 0, 0);duration = 0;timer.Enabled = true;IsRecordingAudio = true;UpdateProgress(duration);}}private void timer_Tick(object sender, ElapsedEventArgs e){duration++;UpdateProgress(duration * 200 / 1000.0);}/// <summary>/// 停止录制/// </summary>/// <param name="isCancle">是否是取消录制,默认为false</param>/// <returns></returns>public void Stop(bool isCancle = false){lock (_obj){if (!IsRecordingAudio) return;timer.Enabled = false;FileUtils.CheckAndCreateFolder();string wavPath = "resource/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".wav";string mp3Path = "resource/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".mp3";string basePath = AppDomain.CurrentDomain.BaseDirectory;mciSendString("stop movie", "", 0, 0);if (!isCancle){mciSendString("save movie " + wavPath, "", 0, 0);}mciSendString("close movie", "", 0, 0);IsRecordingAudio = false;int s = duration * 200;RecordingFinish(s % 1000 == 0 ? s / 1000 : s / 1000 + 1, basePath + mp3Path, isCancle);}}} 



这里录制完成时wav文件,某些需求可能是需要装换为MP3格式,针对音频装换也有开源的代码。

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台