zoukankan      html  css  js  c++  java
  • c# NAudio 录音功能实现

    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, 0, e.BytesRecorded);

                int secondsRecorded = (int)mWavWriter.Length / mWavWriter.WaveFormat.AverageBytesPerSecond;

            }

        }

    简单的调用代码。

        RecordController record = new RecordController();

           

       private void button1_Click(object sender, EventArgs e)

            {

                record.StartRecord("e:\rec.wav");

            }

            private void button2_Click(object sender, EventArgs e)

            {

                record.StopRecord();

            }

  • 相关阅读:
    控制器之间的通信(传值方法)以及多次调用通信
    关于ios项目沙盒中的文件和Xcode项目创建的文件
    解决cell循环利用造成的重复勾选
    让TabelView视图中自定义的Toolbar固定(不随cell的移动而移动)
    jsonString转NSDictionary
    日期字符串转换 and 两个日期相减
    Java虚拟机 简介
    浅谈操作系统对内存的管理(转)
    Java虚拟机规范(Java SE 7)笔记
    StringUtils
  • 原文地址:https://www.cnblogs.com/bruce1992/p/14681814.html
Copyright © 2011-2022 走看看