C#

2011-08-08 11 views
5

kullanarak NAudio ile kayıt NAudio kullanarak C# ses kaydetmeye çalışıyorum. NAudio Chat Demo'ya baktıktan sonra kayıt yapmak için bazı kodları kullandım.C#

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); 
    } 
} 

Ancak eventhandler çağrıldığını değildir: Burada

kodudur. Ben .NET sürümü 'v2.0.50727' kullanılarak ve olarak derlenmesi ediyorum: Bu o zaman bir message loop eksik, bütün kod ise

csc file_name.cs /reference:Naudio.dll /platform:x86 

cevap

5

. Tüm eventHandler özel olayları bir mesaj döngüsü gerektirir. İhtiyacınıza göre Application veya Form referanslarını ekleyebilirsiniz.

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); 
    } 
} 
+3

Evet, ileti döngüsü eksikliği sorunudur. Alternatif bir düzeltme, işlev geri çağrılarını kullanmaktır. –

+0

Formatı MP3 olarak kaydetmek için dönüştürebilir miyiz? – 7addan