zoukankan      html  css  js  c++  java
  • WPF使用NAudio录音

    代码:

    using NAudio.Wave;
    using System.Windows;
    
    namespace NAudioDemo
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            RecordController record = new RecordController();
            bool startRecord = false;
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (!startRecord)
                {
                    record.StartRecord("D:\rec.wav");
                    startRecord = true;
                }
                else
                {
                    record.StopRecord();
                    startRecord = false;
                }
            }
        }
    
        /// <summary>
        /// 录音功能控制类
        /// </summary>
        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;
            }
        }
    }
  • 相关阅读:
    Discuz!X3.1插件创建与配置
    Discuz!X3.1如何设置下载附件扣除积分
    Discuz!X3.1标签使用(二)
    Discuz!X3.1标签使用(一)
    Discuz!X3.1后台操作技巧(整合)
    Discuz!X3.1数据库的操作(三)
    Discuz!X3.1数据库的操作(二)
    Discuz!X3.1数据库的操作(一)
    Discuz!X3.2导航下拉菜单样式修改
    【discuz x3】源代码中的sql调用
  • 原文地址:https://www.cnblogs.com/wzwyc/p/7380265.html
Copyright © 2011-2022 走看看