使用C#与n音讯录音

使用C#与n音讯录音

本文介绍了使用C#与n音讯录音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录使用n音讯C#音频。 。看n音讯聊天演示之后,我使用了一些代码,从有到记录



下面是代码:

 使用系统; 
使用NAudio.Wave;

公共类FOO
{
静态WaveIn s_WaveIn;

静态无效的主要(字串[] args)
{
的init();
,而(真)/ *呀,这是不好的,但只是为了测试.... * /
System.Threading.Thread.Sleep(3000);
}

公共静态无效的init()
{
s_WaveIn =新WaveIn();
s_WaveIn.WaveFormat =新WAVEFORMAT(44100,2);

s_WaveIn.BufferMilliseconds = 1000;
s_WaveIn.DataAvailable + =新的EventHandler< WaveInEventArgs>(SendCaptureSamples);
s_WaveIn.StartRecording();
}

静态无效SendCaptureSamples(对象发件人,WaveInEventArgs E)
{
Console.WriteLine(字节记录:{0},e.BytesRecorded);
}
}



然而,事件处理程序不会被调用。我使用的.NET版本V2.0.50727和编译它为:

  CSC file_name.cs /reference:Naudio.dll /平台:86 


解决方案

如果这是你的整个代码,那么你就错过了消息循环。所有事件处理具体事件需要一个消息循环。您可以添加一个引用应用表格根据自己的需要。



下面是使用的例子表格

 使用系统;使用System.Windows.Forms的
;
使用的System.Threading;
使用NAudio.Wave;

公共类FOO
{
静态WaveIn s_WaveIn;

[STAThread]
静态无效的主要(字串[] args)
{
线程线程=新主题(委托(){
的init() ;
Application.Run();
});

thread.Start();

Application.Run();
}

公共静态无效的init()
{
s_WaveIn =新WaveIn();
s_WaveIn.WaveFormat =新WAVEFORMAT(44100,2);

s_WaveIn.BufferMilliseconds = 1000;
s_WaveIn.DataAvailable + =新的EventHandler< WaveInEventArgs>(SendCaptureSamples);
s_WaveIn.StartRecording();
}

静态无效SendCaptureSamples(对象发件人,WaveInEventArgs E)
{
Console.WriteLine(字节记录:{0},e.BytesRecorded);
}
}


I am trying to record audio in C# using NAudio. After looking at the NAudio Chat Demo, I used some code from there to record.

Here is the code:

using System;
using NAudio.Wave;

public class FOO
{
    static WaveIn s_WaveIn;

    static void Main(string[] args)
    {
        init();
        while (true) /* Yeah, this is bad, but just for testing.... */
            System.Threading.Thread.Sleep(3000);
    }

    public static void init()
    {
        s_WaveIn = new WaveIn();
        s_WaveIn.WaveFormat = new WaveFormat(44100, 2);

        s_WaveIn.BufferMilliseconds = 1000;
        s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
        s_WaveIn.StartRecording();
    }

    static void SendCaptureSamples(object sender, WaveInEventArgs e)
    {
        Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
    }
}

However, the eventHandler is not being called. I am using .NET version 'v2.0.50727' and compiling it as:

csc file_name.cs /reference:Naudio.dll /platform:x86
解决方案

If this is your whole code, then you are missing a message loop. All the eventHandler specific events requires a message loop. You can add a reference to Application or Form as per your need.

Here is an example by using Form:

using System;
using System.Windows.Forms;
using System.Threading;
using NAudio.Wave;

public class FOO
{
    static WaveIn s_WaveIn;

    [STAThread]
    static void Main(string[] args)
    {
        Thread thread = new Thread(delegate() {
            init();
            Application.Run();
        });

        thread.Start();

        Application.Run();
    }

    public static void init()
    {
        s_WaveIn = new WaveIn();
        s_WaveIn.WaveFormat = new WaveFormat(44100, 2);

        s_WaveIn.BufferMilliseconds = 1000;
        s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
        s_WaveIn.StartRecording();
    }

    static void SendCaptureSamples(object sender, WaveInEventArgs e)
    {
        Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
    }
}

这篇关于使用C#与n音讯录音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:03