Click here to Skip to main content
15,918,404 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello

I have a record function which let me to record a vocal 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 install an external sound card the function stops recording sound from my default sound card and it records 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
Posted
Updated 17-Jun-11 11:46am
v3
Comments
Graham Shanks 17-Jun-11 17:45pm    
Edited to use pre tags (not code tags)
Christian Graus 17-Jun-11 18:31pm    
Surely when you enumarate audio end points, that will find both cards and you can choose ?
Nicole castel 17-Jun-11 18:43pm    
No I'm just enumerating microphones and I didn't give the user the possibility to choise between card sounds because I want it to record sound only from the default device

1 solution

I think this might help you :
C# MP3 Sound Capturing/Recording Component[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900