C# NAudio 录音、播放录音

1、录音

源代码地址GIT:https://github.com/markheath/voicerecorder

public class Recorder
{
    private int deviceNumber = 0;    // 选择的录音设备下标。多个设备时可设置为用户选择
    private WaveIn waveIn;    // waveIn操作类
    private WaveFormat = recordingFormat;    // 录音格式
    private WaveFileWriter writer;    // 录音文件操作类
    public event EventHandler StoppedEvent = delegate { };    // 录音结束事件
    public event EventHandler DataAvailableEvent = delegate { }; // 录音过程中接收到数据事件
    public double RecordedTime        // 获取到录音的时长
    {
        get
        {
            if (writer == null)
                return 0;
            return (double)writer.Length / writer.WaveFormat.AverageBytesPerSecond;
        }
    }
    
    /// 
    /// 开始录音
    /// 
    /// 保存的文件名
    internal bool StartRecorder(string filename)
    {
        // 设置录音格式
        recordingFormat = new WaveFormat(44100, WaveIn.GetCapabilities(deviceNumber).Channels);
        // 设置麦克风操作对象
        waveIn = new WaveIn();
        waveIn.DeviceNumber = deviceNumber;    // 设置使用的录音设备
        waveIn.DataAvailable += OnDataAviailable;        // 接收到音频数据时,写入文件
        waveIn.RecordingStopped += OnRecordingStopped;   // 录音结束时执行
        waveIn.WaveFormat = recordingFormat;
        // 设置文件操作类
        writer = new WaveFileWriter(filename, recordingFormat);
        // 开始录音
        waveIn.StartRecording();

        return true;
    }

    /// 
    /// 结束录音
    /// 
    /// 
    internal bool StopRecorder()
    {
        waveIn.StopRecording();
        return true;
    }

    /// 
    /// 录音结束回调函数
    /// 
    /// 
    /// 
    private void OnRecordingStopped(object sender, StoppedEventArgs e)
    {
        writer.Dispose();
        // 通知结束事件
        StoppedEvent(this, EventArgs.Empty);
    }

    /// 
    /// 录音回调函数,写入数据
    /// 
    /// 
    /// 
    private void OnDataAviailable(object sender, WaveInEventArgs e)
    {
        byte[] buffer = e.Buffer;
        int bytesRecorded = e.BytesRecorded;
        WriteToFile(buffer, bytesRecorded);    // 音频数据写入文件
        DataAvailableEvent(this, EventArgs.Empty);
    }

    /// 
    /// 写入文件
    /// 
    /// 
    /// 
    private void WriteToFile(byte[] buffer, int bytesRecorded)
    {
        long maxFileLength = this.recordingFormat.AverageBytesPerSecond * 60;

        var toWrite = (int)Math.Min(maxFileLength - writer.Length, bytesRecorded);
        if (toWrite > 0)
        {
            writer.Write(buffer, 0, bytesRecorded);
        }
        else
        {
            StopRecorder();
        }
    }
}

2、播放录音

public class Recorder
{
    private WaveOut = null;    // 音频播放操作类
    private AudioFileReader waveReader;    // 音频文件读取类
    private float playVolume = 1f;    // 音频播放的音量大小 0~1
    public event EventHandler PlayStoppedEvent = delegate { };    // 音频播放结束事件

    /// 
    /// 设置音量
    /// 
    /// 
    internal void SetPlayVolume(float volume)
    {
        playVolume = volume;
        if (null != waveOut)
        {
            // 支持在播放的过程中实时的修改音量
            waveOut.Volume = playVolume;
        }
    }

    /// 
    /// 播放音频
    /// 
    /// 音频文件路径
    /// 
    internal List PlayWav(string filename)
    {
        List devs = new List();
        for (int i = 0; i < WaveOut.DeviceCount; i++)
        {
            devs.Add(WaveOut.GetCapabilities(i).ProductName);
        }
        if (devs.Count < 1)
        {
            devs.Add("当前设备不支持扬声器");
            return devs;
        }
        // 加载音频文件数据
        waveReader = new AudioFileReader(filename);
        waveReader.Position = 0;
        // 初始化音频播放操作类
        waveOut = new WaveOut();
        waveOut.Init(waveReader);
        waveOut.Volume = playVolume;    // 音量
        waveOut.PlaybackStopped += OnPlaybackStopped;    // 播放停止事件
        waveOut.Play();

        return new List();
    }

    /// 
    /// 播放停止事件
    /// 
    /// 
    /// 
    private void OnPlaybackStopped(object sender, StoppedEventArgs e)
    {
        PlayStoppedEvent.Invoke(this, EventArgs.Empty);
    }
}

 

你可能感兴趣的:(C#)