NAudio kitaplığı kullanılarak bir ComboBox nesnesinde korunan ses aygıtlarından birini seçerken, seçili aygıttan ses algılanırsa example.exe’yi kapatırım?
waveIn.DeviceNumber = selectedDevice.DeviceNumber; // hata kısmı bu konun sonundaki (DeviceNumber) çalışmasına engel oluyor bunu nasıl düzeltebilirim?
using NAudio.CoreAudioApi;
private void Form1_Load(object sender, EventArgs e)
{
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
MMDeviceCollection devices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
foreach (MMDevice device in devices)
{
comboBox1.Items.Add(device.FriendlyName);
}
}
private MMDevice selectedDevice;
private WaveIn waveIn;
private bool soundDetected = false;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
selectedDevice = new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)[comboBox1.SelectedIndex];
waveIn = new WaveIn();
waveIn.DeviceNumber = selectedDevice.DeviceNumber; //error selectedDevice.DeviceNumber;(DeviceNumber)
waveIn.WaveFormat = new WaveFormat(44100, 1);
waveIn.DataAvailable += OnDataAvailable;
waveIn.StartRecording();
timer1.Start();
}
private void OnDataAvailable(object sender, WaveInEventArgs e)
{
float max = 0;
byte[] buffer = e.Buffer;
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((buffer[index + 1] << 8) | buffer[index + 0]);
float sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}
if (max > 0.5f)
{
soundDetected = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (soundDetected)
{
soundDetected = false;
timer1.Stop();
waveIn.StopRecording();
waveIn.Dispose();
this.Close();
}
}