Recording stopped after waveout is stopped naudio c#

I have a windows forms application that does multiple recording from multiple microphone inputs. I listen to the input one after the other, using a WaveInProvider and DirectSoundOut with a variable called waveOut, on using the waveOut.Play().

This works, but when I click the button to stop listening using waveOut.Stop() and Dispose, the recording stops too.

Please how can I stop this behaviour, so that the recording does not stop.

My code is below Thanks:

private List<Label> lblRecordingStatus = new List<Label>();
private DirectSoundOut waveOut = null;

void RecordMicNAudio(int deviceNum, IProgress<int> progress, string StationName, WaveFileWriter wfWriter, WaveInEvent waveSource, Label lblCurrentStatus)
    {
        try
        {
            //waveSource = new WaveInEvent();              
            SetControlText(lblCurrentStatus, "Recording...");

            waveSource.DeviceNumber = deviceNum;
            waveSource.WaveFormat = new WaveFormat(sampleRate, bits: 16, WaveIn.GetCapabilities(deviceNum).Channels);
            waveSource.DataAvailable += (_, e) =>
            {
                if (wfWriter == null)
                {
                    startTime = DateTime.Now;
                    wfWriter = new WaveFileWriter(StationName + " " + startTime.ToString("yyyy-MM-dd_HH-mm-ss") + ".wav", waveSource.WaveFormat);
                }
                else
                {
                    TimeSpan elapsed = DateTime.Now - startTime;
                    //elapsed.TotalHours >= 1
                    if (elapsed.TotalMinutes >= 5)
                    {
                        wfWriter.Dispose();
                        wfWriter = null;
                    }
                }
                if (wfWriter != null)
                {
                    wfWriter.Write(e.Buffer, 0, e.BytesRecorded);
                    wfWriter.Flush();
                }

                //Volume Meter
                if (progress != null)
                {
                    int bytesPerSample = waveSource.WaveFormat.BitsPerSample / 8;
                    int newSampleCount = e.BytesRecorded / bytesPerSample;
                    double[] buffer = new double[newSampleCount];

                    double peak = 0;
                    for (int i = 0; i < newSampleCount; i++)
                    {
                        buffer[i] = BitConverter.ToInt16(e.Buffer, i * bytesPerSample);
                        peak = Math.Max(peak, buffer[i]);
                    }

                    var prp = peak / (1 << 15);
                    var pVal = (int)(prp * 100);

                    if (IsHandleCreated)
                    {
                        if(InvokeRequired)
                        {
                            progress.Report(pVal);
                        }
                        else
                            progress.Report(pVal);
                    }
                        
                }
            };
            waveSource.RecordingStopped += (_, _) =>
            {
                if (waveSource != null)
                {                        
                    waveSource.Dispose();
                    waveSource = null;
                }

                if (wfWriter != null)
                {
                    wfWriter.Dispose();
                    wfWriter = null;
                }

                SetControlText(lblCurrentStatus, "Recording Stopped!!!");
            };

            waveSource.StartRecording();
        }
        catch (Exception ex)
        {
            SetControlText(lblCurrentStatus, "Error Occured!" + ex.Message);
        }
    }


private void btnSpeakerOn_Click(object sender, EventArgs e)
    {
        //Get Wavesource parameter, to determine which wavesource to listen to
        Button btn = sender as Button;
        var extractedString = Regex.Match(btn.Name, @"\d+").Value;
        var index = !string.IsNullOrEmpty(extractedString) ? Convert.ToInt32(extractedString) : 0;

        foreach (var item in btnSpeak)
        {
            if (item == btnSpeak[index])
            { 
                item.Visible = false;
                btnMute[index].Visible = true;
            }
            else
                item.Enabled = false;
        }

        //Start listening to waveSource
        _ = Task.Run(() =>
        {
            //producers[index] is the WaveInEvent waveSource currently passed into the method
            WaveInProvider waveIn = new WaveInProvider(producers[index]);
            waveOut = new DirectSoundOut();
            waveOut.Init(waveIn);

            waveOut.Play();
        });
    }

    private void btnSpeakerOff_Click(object sender, EventArgs e)
    {
        try
        {
            if (waveOut != null)
            {
                foreach (var item in btnSpeak)
                {
                    item.Visible = true;
                    item.Enabled = true;
                }

                waveOut.Stop();
                waveOut.Dispose();
                waveOut = null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation