0

我正在尝试从声卡发出的音频创建一个录音机,这是我到目前为止的进展,问题是保存到文件时录制的音频太大,就像一首歌曲可以达到数百兆字节。

这是我的代码

using NAudio.CoreAudioApi;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Record_From_Soundcard
{
    public partial class frmMain : Form
    {
        private WaveFileWriter writer;
        private WasapiLoopbackCapture waveInSel;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
            MMDeviceCollection deviceCol = deviceEnum.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);

            cboAudioDrivers.DataSource = deviceCol.ToList();
        }       

        private void btnStopRecord_Click(object sender, EventArgs e)
        {
            waveInSel.StopRecording();
            writer.Close();
        }        

        private void btnStartRecord_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog _sfd = new SaveFileDialog())
            {
                _sfd.Filter = "Mp3 File (*.mp3)|*.mp3";               

                if (_sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    MMDevice _device = (MMDevice)cboAudioDrivers.SelectedItem;
                    waveInSel = new WasapiLoopbackCapture(_device);
                    writer = new WaveFileWriter(_sfd.FileName, waveInSel.WaveFormat);                    

                    waveInSel.DataAvailable += (n, m) =>
                    {
                        writer.Write(m.Buffer, 0, m.BytesRecorded);
                    };

                    waveInSel.StartRecording();
                }
            }
        }
    }

}

谁能帮助我在保存时如何压缩音频?

也许会在这部分添加

waveInSel.DataAvailable += (n, m) =>
    {
         writer.Write(m.Buffer, 0, m.BytesRecorded);
    };

提前致谢.. ;)

4

2 回答 2

2

尝试使用 naudio dll

Using NAudio.Wave; Using NAudio.Wave.SampleProviders;Using NAudio.Lame
Private void WaveToMP3(int bitRate = 128){
    String waveFileName = Application.StartupPath + @"\Temporal\mix.wav";
    String mp3FileName = Application.StartupPath + @"\Grabaciones\ " + Strings.Format(DateTime.Now, "dd-MM-yyyy.HH.mm.ss") + ".mp3";

    Using (var reader = New AudioFileReader(waveFileName))
    {
        Using (var writer = New LameMP3FileWriter(mp3FileName, reader.WaveFormat, bitRate))
        {
            reader.CopyTo(writer);
        }
        reader.Close();
    }
}
于 2021-10-22T16:34:39.833 回答
0

您不能通过保存扩展名为 .MP3 的 WAV 文件来制作 MP3 文件(这就是您在此处所做的)。您需要选择机器上可用的编码器,然后通过它传递音频。我将在本文中详细介绍如何使用 NAudio 执行此操作。

于 2013-03-11T09:45:01.143 回答