c#NAudio 录音功能实现

c#NAudio 录音功能实现,第1张

概述在网上找了很多类似录音教程效果都不好,或根本不能录音,代码由网上借鉴修改(完整实现录音播放功能) 1.首先新建引用类  RecordController public class RecordController { public WaveIn mWavIn; public WaveFileWriter mWavWriter; /// <summary> /// 开始录音 /// </summar

在网上找了很多类似录音教程效果都不好,或根本不能录音,代码由网上借鉴修改(完整实现录音播放功能)

1.首先新建引用类  RecordController

public class RecordController
{
public WaveIn mWavIn;
public WavefileWriter mWavWriter;

/// <summary>
/// 开始录音
/// </summary>
/// <param name="filePath"></param>
public voID StartRecord(string filePath)
{
mWavIn = new WaveIn();
mWavIn.DataAvailable += MWavIn_DataAvailable;
// mWavIn.RecordingStopped += MWavIn_RecordingStopped; 有冲突
mWavWriter = new WavefileWriter(filePath,mWavIn.WaveFormat);
mWavIn.StartRecording();
}

/// <summary>
/// 停止录音
/// </summary>
public voID StopRecord()
{
mWavIn?.StopRecording();
mWavIn?.dispose();
mWavIn = null;
mWavWriter?.Close();
mWavWriter = null;
}

//这个方法在调用关闭时会有冲突

private voID MWavIn_RecordingStopped(object sender,StoppedEventArgs e)
{
//mWavIn?.dispose();
//mWavIn = null;
//mWavWriter?.Close();
//mWavWriter = null;
}

private voID MWavIn_DataAvailable(object sender,WaveInEventArgs e)
{
mWavWriter.Write(e.Buffer,e.BytesRecorded);
int secondsRecorded = (int)mWavWriter.Length / mWavWriter.WaveFormat.AverageBytesPerSecond;
}
}

 

2.在主界面引用  RecordController record = new RecordController();

/// <summary>
/// 耳机sIDetone测试
/// </summary>
/// <param name="i"></param>
public voID SIDetone(int i)
{
string[] row = getListvIEwitem(i);
record.StartRecord("e:\\rec.wav");
if (MessageBox.Show("正在录音,点击是播放录音","提示",MessageBoxbuttons.YesNo,MessageBoxIcon.information) == DialogResult.No)
{
record.StopRecord();
ErrorFlag = false;
row[6] = "FAIL";
}
else
{
record.StopRecord();
string path = "e:\\rec.wav";//.wav音频文件路径
System.Media.soundplayer player = new System.Media.soundplayer(path);
player.Play();

if (MessageBox.Show("是否有录音",MessageBoxIcon.information) == DialogResult.No)
{
ErrorFlag = false;
row[6] = "FAIL";
}
else
{
ErrorFlag = true;
row[5] = "OK";
row[6] = "PASS";
}
}
Listupdate(row,i);
}

完成代码后测试是能正常录音

总结

以上是内存溢出为你收集整理的c#NAudio 录音功能实现全部内容,希望文章能够帮你解决c#NAudio 录音功能实现所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1222933.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存