WPF NAudio录音和播放音频文件-实时绘制音频波形图


    
        
               
        

    

后台部分源码:
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
NAudioRecorder recorder;
NAudioReader aggregator;
ObservableCollection recordList;
WaveOut waveOutDevice;
int recordTime;
string currentState;
string outputFolder;
object lockList = new object();
bool boolPlay = false;
string strPlayAddress;

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

    /// 
    /// 录音时长
    /// 
    public int RecordTime
    {
        get { return recordTime; }
        set
        {
            recordTime = value;
            this.OnPropertyChanged("RecordTime");
        }
    }

    /// 
    /// 录音文件列表
    /// 
    //public ObservableCollection RecordList
    //{
    //    get { return recordList; }
    //    set
    //    {
    //        recordList = value;
    //        this.OnPropertyChanged("RecoredList");
    //    }
    //}

    /// 
    /// 界面正在执行任务状态
    /// 
    public string CurrentState
    {
        get { return currentState; }
        set
        {
            currentState = value;
            this.OnPropertyChanged("CurrentState");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
            this.Closed += MainWindow_Closed;

        waveOutDevice = new WaveOut();
        //RecordList = new ObservableCollection();
        recorder = new NAudioRecorder();

        outputFolder = System.Windows.Forms.Application.StartupPath+ "\\RecoredList\\";
        Directory.CreateDirectory(outputFolder);

        //analysis.IsEnabled = false;
        btnBoFang.IsEnabled = false;
        endRecord.IsEnabled = false;
    }

    private void Recorder_MicrophoneVoiceReceived(object sender, MaxSampleEventArgs f)
    {
        polylineWaveFormControl.AddValue(f.MaxSample);
    }

    /// 
    /// 退出程序时触发,释放相关资源
    /// 
    /// 
    /// 
    private void MainWindow_Closed(object sender, EventArgs e)
    {
        if (waveOutDevice != null)
        {
            waveOutDevice.PlaybackStopped -= WaveOutDevice_PlaybackStopped;
            waveOutDevice.Dispose();
        }
        if (recorder != null)
        {
            recorder.StopRec();
        }
        if (aggregator != null)
        {
            aggregator.Stop();
        }
    }

    /// 
    /// 开始录音
    /// 
    /// 
    /// 
    private void StartRecord_Click(object sender, RoutedEventArgs e)
    {
        CurrentState = "开始录音";
        var currentFileName = String.Format("NAudioDemo {0:yyy-MM-dd HH-mm-ss}.wav", DateTime.Now);
        var path = Path.Combine(outputFolder, currentFileName);
        recorder.DataAvailable += Recorder_DataAvailable;
        recorder.MaximumCalculated += Recorder_MicrophoneVoiceReceived;
        recorder.SetFileName(path);
        recorder.StartRec();
        lock (lockList)
        {
           // RecordList.Clear();//清空数组
            //RecordList.Add(path);
            strPlayAddress = path;
        }

        endRecord.IsEnabled = true;
        startRecord.IsEnabled = false;
    }

    /// 
    /// 停止录音
    /// 
    /// 
    /// 
    private void EndRecord_Click(object sender, RoutedEventArgs e)
    {
        CurrentState = "结束录音";
        recorder.StopRec();
        //analysis.IsEnabled = true;
        btnBoFang.IsEnabled = true;
        startRecord.IsEnabled = true;
        endRecord.IsEnabled = false;
    }

    /// 
    /// 分析本地音频文件
    /// 
    /// 
    /// 
    private void AnalysisRecord_Click(object sender, RoutedEventArgs e)
    {
        btnBoFang.IsEnabled = false;
        CurrentState = "正在播放录音";
        var inputStream = new AudioFileReader(strPlayAddress); // recordList[0]);
        aggregator = new NAudioReader(inputStream);
        aggregator.MaximumCalculated += Aggregator_MaximumCalculated;
        aggregator.Start();
        waveOutDevice.Init(aggregator);
        waveOutDevice.Volume = 1;
        waveOutDevice.PlaybackStopped += WaveOutDevice_PlaybackStopped;
        waveOutDevice.Play();
    }

    private void Aggregator_MaximumCalculated(object sender, MaxSampleEventArgs e)
    {
        polylineWaveFormControl.AddValue(e.MaxSample);

    }

    /// 
    /// 进度条管理
    /// 
    /// 
    private void Recorder_DataAvailable(int value)
    {
        this.RecordTime = value * 10;
        if (value == 10)//倒计时十秒
        {
            recorder.DataAvailable -= Recorder_DataAvailable;
            recorder.MaximumCalculated -= Recorder_MicrophoneVoiceReceived;
            recorder.StopRec();
            recorder = new NAudioRecorder();

            EndRecord_Click(this, new RoutedEventArgs()); //停止录音
            // StartRecord_Click(this, new RoutedEventArgs()); //重新开始录音
        }
    }
    
    /// 
    /// 音频文件播放结束触发事件
    /// 
    /// 
    /// 
    private void WaveOutDevice_PlaybackStopped(object sender, StoppedEventArgs e)
    {
        waveOutDevice.PlaybackStopped -= WaveOutDevice_PlaybackStopped;
        //RecordList.RemoveAt(0);

        //if(recordList.Count > 0)
        //{
        //    AnalysisRecord_Click(null, null);
        //}
        //else
        //{
            CurrentState = "分析录音完成";
            aggregator.Stop();
        btnBoFang.IsEnabled = true;
        //}
    }
}

}

最终效果图
WPF NAudio录音和播放音频文件-实时绘制音频波形图_第1张图片
源码下载
https://download.csdn.net/download/li_shidong/12241607
添加链接描述

你可能感兴趣的:(WPF NAudio录音和播放音频文件-实时绘制音频波形图)