This project has moved. For the latest updates, please go here.

Problem with a record function

Jun 17, 2011 at 10:41 PM

Hello
 
I have a record function wich let me to record a vaocal message and to save it in my pc then it gives us the possibility to hear the recorded message,the problem is that when I instal an external sound card the function stops recording sound from my default sound card and it recorded it from the other audio card,so can you give me an idea to oblige the record function to use only the default sound card.this is the record code:

public partial class RecordingForm : Form
{
IWaveIn waveIn;
WaveFileWriter writer;
string outputFilename;
public RecordingForm()
{
InitializeComponent();
//LoadWasapiDevicesCombo();
}
private void LoadWasapiDevicesCombo()
{
MMDeviceEnumerator deviceEnum = new MMDeviceEnumerator();
MMDeviceCollection deviceCol = deviceEnum.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
Collection<MMDevice> devices = new Collection<MMDevice>();
foreach (MMDevice device in deviceCol)
{
devices.Add(device);
}
this.comboDevices.DataSource = devices;
this.comboDevices.DisplayMember = "FriendlyName";
}
private void buttonStartRecording_Click(object sender, EventArgs e)
{
if (waveIn == null)
{
if (outputFilename == null)
{
buttonSelectOutputFile_Click(sender, e);
}
if (outputFilename == null)
{
return;
}
waveIn = new WaveIn();
waveIn.WaveFormat = new WaveFormat(8000, 1);
writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(waveIn_DataAvailable);
waveIn.RecordingStopped += new EventHandler(waveIn_RecordingStopped);
waveIn.StartRecording();
buttonStartRecording.Enabled = false;
}
}
void waveIn_RecordingStopped(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new EventHandler(waveIn_RecordingStopped), sender, e);
}
else
{
waveIn.Dispose();
waveIn = null;
writer.Close();
writer = null;
buttonStartRecording.Enabled = true;
progressBar1.Value = 0;
if (checkBoxAutoPlay.Checked)
{
Process.Start(outputFilename);
}
}
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable), sender, e);
}
else
{
writer.WriteData(e.Buffer, 0, e.BytesRecorded);
int secondsRecorded = (int)(writer.Length / writer.WaveFormat.AverageBytesPerSecond);
if (secondsRecorded >= 30)
{
StopRecording();
}
else
{
progressBar1.Value = secondsRecorded;
}
}
}
void StopRecording()
{
Debug.WriteLine("StopRecording");
waveIn.StopRecording();
}
private void buttonStopRecording_Click(object sender, EventArgs e)
{
if (waveIn != null)
{
StopRecording();
}
}
private void buttonSelectOutputFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "Select output file:";
saveFileDialog.Filter = "WAV Files (*.wav)|*.wav";
saveFileDialog.FileName = outputFilename;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
outputFilename = saveFileDialog.FileName;
}
} 
}
}

 


 
Thank you in advance and good day

Coordinator
Jun 19, 2011 at 8:06 AM

it is already using the default soundcard - that's what opening WaveIn with device number = 0 does. Your trouble is that when you plug a USB soundcard into Windows, Windows makes that soundcard the default device.

Jul 10, 2011 at 10:49 PM

How does one get the DeviceNumber from an MMDevice?

Cheers!

Luke

Coordinator
Jul 11, 2011 at 9:58 AM

to explore WaveIn devices, use the WaveIn.DeviceCount static member to find out how many there are, and then pass in an index WaveIn.GetCapabilities to find out device name etc.

Same concept for WaveOut too.

Mark

Oct 13, 2011 at 6:48 AM
Edited Oct 13, 2011 at 6:48 AM

so it is still not clear what do I do with device name... do I need then to find the recording default device (I recently found some COM code that does that) and compare the names? if the names match use the waveIn.DeviceNumber with appropriate index that corresponds to the matching name?

Coordinator
Oct 17, 2011 at 10:39 AM

No, just use device 0 and that is the default recording device