C# 保存声音 录音

C#winform中保存设备返回的PCM音频。自己写文件,而不是调用其他的方法。


文中参考了这篇博文  http://blog.csdn.net/woaixiaozhe/article/details/7852824

原文是对声卡进行录音,这里是直接将录音改为一个类,直接调用就好了


录音类:

using System.IO;

    /// <summary>
    /// 生成录音文件
    /// BY SUMC
    /// 2015-10-16
    /// </summary>
    public class rec
    {
        private WaveFormat mWavFormat;
        private int mSampleCount = 0;

        private string mFileName = "";
        private FileStream mWaveFile = null;
        private BinaryWriter mWriter = null;

        public rec(string audioFileName)
        {
            // 设定录音格式  
            this.mFileName = audioFileName;
            mWavFormat = CreateWaveFormat();
        }

        private WaveFormat CreateWaveFormat()
        {
            WaveFormat format = new WaveFormat();
            format.SamplesPerSecond = 32000;
            format.BitsPerSample = 16; 
            format.Channels = 1;
            format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));   
            format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
            return format;
        }

        public void RecStop()
        {
            // 写WAV文件尾  
            mWriter.Seek(4, SeekOrigin.Begin);
            mWriter.Write((int)(
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在C#中设置时间录音,可以使用Windows提供的Win32 API函数来控制录音设备。以下是一个简单的示例: ```csharp using System; using System.Runtime.InteropServices; using System.Threading; class Program { // Win32 API函数声明 [DllImport("winmm.dll", SetLastError = true)] static extern uint timeSetEvent(uint uDelay, uint uResolution, TimerCallback lpTimeProc, IntPtr dwUser, uint fuEvent); [DllImport("winmm.dll", SetLastError = true)] static extern uint timeKillEvent(uint uTimerID); [DllImport("winmm.dll", SetLastError = true)] static extern uint mciSendString(string lpstrCommand, string lpstrReturnString, uint uReturnLength, IntPtr hwndCallback); // 录音参数 const int SampleRate = 44100; const int BitsPerSample = 16; const int Channels = 2; static void Main(string[] args) { // 设置录音时长为5秒 int duration = 5 * 1000; // 准备录音 string command = $"open new type waveaudio alias recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音格式 command = $"set recsound format tag pcm bitspersample {BitsPerSample} channels {Channels} samplespersec {SampleRate}"; mciSendString(command, null, 0, IntPtr.Zero); // 开始录音 command = $"record recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 设置录音时长 uint callbackId = timeSetEvent((uint)duration, 0, TimerCallback, IntPtr.Zero, 1); // 等待录音结束 Thread.Sleep(duration); // 停止录音 command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 取消时间事件 timeKillEvent(callbackId); } // 时间事件回调函数 static void TimerCallback(uint uTimerID, uint uMsg, IntPtr dwUser, IntPtr dw1, IntPtr dw2) { // 停止录音 string command = $"stop recsound"; mciSendString(command, null, 0, IntPtr.Zero); // 关闭录音 command = $"close recsound"; mciSendString(command, null, 0, IntPtr.Zero); } } ``` 在上面的示例中,我们使用timeSetEvent函数来设置录音时长,然后使用mciSendString函数来准备录音设备,设置录音格式,开始录音,停止录音和关闭录音。在时间事件回调函数中,我们停止录音并关闭录音设备。 请注意,由于使用了Win32 API函数,需要添加System.Runtime.InteropServices命名空间,并且该代码只能在Windows操作系统上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值