[英]recording microphone audio using NAudio in a console application
my code runs and creates a test.wav but this file dosent contain aything. 我的代码运行并创建一个test.wav但这个文件包含一些东西。 i am trying to run this code in a console application.
我试图在控制台应用程序中运行此代码。 please help
请帮忙
using System;
using System.Media;
using NAudio;
using NAudio.Wave;
class sound
{
public static void Main()
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
Console.WriteLine("Now recording...");
WaveInEvent waveSource = new WaveInEvent();
waveSource.DeviceNumber = 0;
waveSource.WaveFormat = new WaveFormat(16000, deviceInfo.Channels);
//waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
string tempFile = (@"C:\Users\user\Desktop\test1.wav");
WaveFileWriter waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
waveSource.StartRecording();
}
//void waveInStream_DataAvailable(object sender, WaveInEventArgs e)
//{
// wavefile.WriteData(e.Buffer, 0, e.BytesRecorded);
//}
}
and can someone please explain what do the lines which are commented mean. 并且有人可以解释被评论的行是什么意思。 i am a beginner in programming.
我是编程的初学者。
when i compile the program it gives 2 errors: Error 1: An object reference is required for the non-static field, method, or property 'sound.waveSource_DataAvailable(object, NAudio.Wave.WaveInEventArgs)' C:\\Users\\user\\Documents\\Visual Studio 2008\\Projects\\sound\\sound\\Program.cs 18 49 sound 当我编译程序时,它给出了2个错误:错误1:非静态字段,方法或属性'sound.waveSource_DataAvailable(object,NAudio.Wave.WaveInEventArgs)'C:\\ Users \\ user \\需要对象引用Documents \\ Visual Studio 2008 \\ Projects \\ sound \\ sound \\ Program.cs 18 49声音
Error 2 The name 'wavefile' does not exist in the current context C:\\Users\\user\\Documents\\Visual Studio 2008\\Projects\\sound\\sound\\Program.cs 28 21 sound 错误2当前上下文中不存在名称'wavefile'C:\\ Users \\ user \\ Documents \\ Visual Studio 2008 \\ Projects \\ sound \\ sound \\ Program.cs 28 21声音
Apparently the StartRecording
methods start some capture loops that periodically raises the DataAvailable
event to allow the user collecting the recorded data. 显然,
StartRecording
方法启动一些捕获循环,定期引发DataAvailable
事件以允许用户收集记录的数据。 In your example code the event handle properly append the recorded data to the file tempFile
. 在示例代码中,事件句柄将记录的数据正确附加到文件
tempFile
。 Both the function waveInStream_DataAvailable
and the waveFile
must be declared as static. 函数
waveInStream_DataAvailable
和waveFile
必须声明为static。
Try this: 尝试这个:
using System;
using System.Media;
using NAudio;
using NAudio.Wave;
class sound
{
static WaveFileWriter waveFile;
public static void Main()
{
//WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
Console.WriteLine("Now recording...");
WaveInEvent waveSource = new WaveInEvent();
//waveSource.DeviceNumber = 0;
waveSource.WaveFormat = new WaveFormat(44100, 1);
waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
string tempFile = (@"C:\Users\user\Desktop\test1.wav");
waveFile = new WaveFileWriter(tempFile, waveSource.WaveFormat);
waveSource.StartRecording();
Console.WriteLine("Press enter to stop");
Console.ReadLine();
waveSource.StopRecording();
waveFile.Dispose();
}
static void waveSource_DataAvailable(object sender, WaveInEventArgs e)
{
waveFile.WriteData(e.Buffer, 0, e.BytesRecorded);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.