因此,我遇到了一些問題,我編寫了一個拍手傳感器,聽到有人拍手並執行某個命令時會聽到。窗口刷新音頻
//CLAP
private float bigValue;
WaveIn waveIn;
private double MaxValue;
private void button1_Loaded(object sender, RoutedEventArgs e)
{
if (Convert.ToInt16(textBox1.Text) > 100)
{
MessageBox.Show("Invalid Value");
return;
}
else
MaxValue = Convert.ToDouble(textBox1.Text)/100;
bigValue = 0;
waveIn = new WaveIn();
int waveInDevices = waveIn.DeviceNumber;
//Get Device Count
for (int waveInDevice = 0; waveInDevice < waveInDevices; waveInDevice++)
{
WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(waveInDevice);
}
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
int sampleRate = 8000;
int channels = 1;
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.StartRecording();
}
//CLAP
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((e.Buffer[index + 1] << 8) | e.Buffer[index + 0]);
float sample32 = sample/32768f;
label1.Content = sample32.ToString();
if (bigValue < sample32)
{
bigValue = sample32;
label2.Content = bigValue.ToString();
if (bigValue > MaxValue)
{
waveIn.StopRecording();
SendMessage(MONITOR_ON, WM_SYSCOMMAND, SC_MONITORPOWER, MONITOR_ON);
MessageBox.Show("Did you Clap?");
}
}
}
}
代碼本身按原樣工作,但我需要它能夠根據需要多次重置自身。這個程序基本上聽一個拍手,喚醒顯示器並啓動它。該程序打破了我添加另一個「waveIn.StartRecording();」的任何時間
關於如何刷新頁面或讓它永遠聽音樂的任何想法?
可能需要將'WaveIn'代碼移動到後端線程([BackgroundWorker](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx)),它只是在檢測到拍手時在主線上觸發事件,但始終保持收聽。 – wdosanjos
你可以給我一個例子,如何做到這一點 –
我會使用WasapiCapture而不是WaveIn。 WaveIn已經過時了。 Wasapi是一個非常新的API,自Windows Vista以來,它在所有Windows系統上都得到支持。 –