I am trying to create an application that records the mic input and puts its volume in a progress bar as a sort of noise-o-meter. I have the audio input devices set and all working but the mic is only receiving microphone input noise when another application is using the same microphone device. When I close the said application, it doesn't read the microphone sound again.
This is my code:
public Form1() {
InitializeComponent();
InitializeAudioDevices();
timer1.Start();
}
// to initialize audio devices in the computer
private void InitializeAudioDevices() {
var waveIn = new WaveIn();
waveIn.StartRecording();
MMDeviceEnumerator enumerator = new MMDeviceEnumerator();
// Initialize MMDeviceEnumerator
// Get the collection of audio capture devices (input devices)
var captureDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
Input.Items.AddRange(captureDevices.ToArray());
var defaultInput = enumerator.GetDefaultAudioEndpoint(DataFlow.Capture, Role.Console);
// Loop through devices to select default Input device
for (int i = 0; i < Input.Items.Count; i++) {
if (Input.Items[i].ToString() == defaultInput.ToString()) {
Input.SelectedIndex = i;
break;
}
}
// Get the collection of audio render devices (output devices)
var renderDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active);
Output.Items.AddRange(renderDevices.ToArray());
var defaultOutput = enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
// Loop through devices to select default Output device
for (int i = 0; i < Output.Items.Count; i++) {
if (Output.Items[i].ToString() == defaultOutput.ToString()) {
Output.SelectedIndex = i;
break;
}
}
}
public static MMDevice singleDevice;
public void timer1_Tick(object sender, EventArgs e) {
if (Input.SelectedItem != null) {
var singleDevice = (MMDevice) Input.SelectedItem;
progressBar1.Value = (int)(singleDevice.AudioMeterInformation.MasterPeakValue * 100);
label3.Text = progressBar1.Value.ToString();
var waveIn = new WaveIn();
waveIn.DeviceNumber = Input.SelectedIndex;
}
}
I tried many ways like trying to use the
var waveIn = new WaveIn();
waveIn.StartRecording();
but it doesn't seem to do the trick or I may be using it wrong.