- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我能够在 WasapiLoopbackCapture (naudio) 的帮助下捕获扬声器生成的系统音频。但问题是它捕获的 wav 文件和 wav 文件的大小非常大(几乎 10 到 15 MB/分钟)。我必须捕获 2-3 小时的音频,这太长了。我正在寻找一种解决方案,将 WasapiLoopbackCapture 捕获的 wav 流转换为 MP3,然后将其保存到磁盘。我尝试使用 LAME.exe 或其他解决方案,但没有成功。任何工作代码。
这是我的代码:
private void button1_Click(object sender, EventArgs e){
LoopbackRecorder obj = new LoopbackRecorder();
string a = textBox1.Text;
obj.StartRecording(@"e:\aman.mp3");
}
public class LoopbackRecorder
{
private IWaveIn _waveIn;
private Mp3WaveFormat _mp3format;
private WaveFormat _wavFormat;
private WaveFileWriter _writer;
private bool _isRecording = false;
/// <summary>
/// Constructor
/// </summary>
public LoopbackRecorder()
{
}
/// <summary>
/// Starts the recording.
/// </summary>
/// <param name="fileName"></param>
public void StartRecording(string fileName)
{
// If we are currently record then go ahead and exit out.
if (_isRecording == true)
{
return;
}
_fileName = fileName;
_waveIn = new WasapiLoopbackCapture();
// _waveIn.WaveFormat = new WaveFormat(16000, 16 , 2);
_writer = new WaveFileWriter(fileName, _waveIn.WaveFormat);
_waveIn.DataAvailable += OnDataAvailable;
// _waveIn.RecordingStopped += OnRecordingStopped;
_waveIn.StartRecording();
_isRecording = true;
}
private void OnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
{
if (_writer == null)
{
_writer = new WaveFileWriter(@"e:\aman.mp3", _waveIn.WaveFormat);
}
_writer.Write(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
byte[] by= Float32toInt16(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
}
private string _fileName = "";
/// <summary>
/// The name of the file that was set when StartRecording was called. E.g. the current file being written to.
/// </summary>
public string FileName
{
get
{
return _fileName;
}
}
}
最佳答案
下面是使用 NAudio.Lame
(在控制台应用程序中)从声卡回送中捕获数据并直接写入 MP3 文件的示例:
using System;
using NAudio.Lame;
using NAudio.Wave;
namespace MP3Rec
{
class Program
{
static LameMP3FileWriter wri;
static bool stopped = false;
static void Main(string[] args)
{
// Start recording from loopback
IWaveIn waveIn = new WasapiLoopbackCapture();
waveIn.DataAvailable += waveIn_DataAvailable;
waveIn.RecordingStopped += waveIn_RecordingStopped;
// Setup MP3 writer to output at 32kbit/sec (~2 minutes per MB)
wri = new LameMP3FileWriter( @"C:\temp\test_output.mp3", waveIn.WaveFormat, 32 );
waveIn.StartRecording();
stopped = false;
// Keep recording until Escape key pressed
while (!stopped)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true);
if (key != null && key.Key == ConsoleKey.Escape)
waveIn.StopRecording();
}
else
System.Threading.Thread.Sleep(50);
}
// flush output to finish MP3 file correctly
wri.Flush();
// Dispose of objects
waveIn.Dispose();
wri.Dispose();
}
static void waveIn_RecordingStopped(object sender, StoppedEventArgs e)
{
// signal that recording has finished
stopped = true;
}
static void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
// write recorded data to MP3 writer
if (wri != null)
wri.Write(e.Buffer, 0, e.BytesRecorded);
}
}
}
目前 NuGet 上的 NAudio.Lame
包仅针对 x86 编译,因此请确保您的应用程序设置为面向该平台。
关于c# - 将 WasapiLoopbackCapture wav 音频流转换为 MP3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19722028/
我终于构建了一个程序来使用 NAudio 收听内部音频环回,并输出可识别的文本。问题是它会听,并且总是说,例如: Recognized text: had Recognized text: had R
我正在尝试从我的声卡44100Hz, 16bit, 2 channel 波形中重新采样 WasapiLoopbackCapture 的输出到 16000Hz, 16bit, 1 channel 格式,
我正在尝试使用WasapiLoopbackCapture类(NAudio 1.7.1.17),并以COMException(0x88890003)结尾。记录格式为WaveFormat(44100, 1
我正在使用 WasapiLoopbackCapture 捕获音频 - format = IeeeFloat- SampleRate = 48000- BitsPerSample =
我能够在 WasapiLoopbackCapture (naudio) 的帮助下捕获扬声器生成的系统音频。但问题是它捕获的 wav 文件和 wav 文件的大小非常大(几乎 10 到 15 MB/分钟)
下面是我录制来自扬声器的音频的示例代码。它工作正常。但只有当某些音频从扬声器中传出时,它才会录制音频。如果没有音频则表示它没有录音。实际上我正在用系统的声音进行屏幕录制。 wasapiloop录制的音
我是一名优秀的程序员,十分优秀!