naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.55k stars 1.1k forks source link

Naudio Recording Fails when Switching Machines. #580

Open TheDJPHD opened 4 years ago

TheDJPHD commented 4 years ago

Hello. I am creating an audio word processor and I am using Naudio to help with the actual recording and WAV file creation. I have got this to work using VS 2019 with the latest Naudio version on my 2 windows 10 machines.

When I give the solution and .exe file to my superior to use on his machine, everything builds and runs fine. VS 2019 - same Naudio version. The Record and Stop button execute their respective code fine, but the program never actually grabs real samples. It will create a however long wav file of 0-valued samples (nothing recorded). It is odd because I can see the device when running on his machine, which is just a RealTek on-board device.

I would like to show you my very simple Record and Stop code, and if anyone has any ideas, I would really appreciate it. For what reason would the code allow me to actually find the device but not actually record any samples?

// Recording Button private void RecordingButton_Click(object sender, EventArgs e) { RecordingButton.Enabled = false; StopButton.Enabled = true;

        try
        {
            waveSource = new WaveInEvent();
            waveSource.WaveFormat = new WaveFormat(16000, 1);

            waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
            waveSource.RecordingStopped += new EventHandler<StoppedEventArgs>(waveSource_RecordingStopped);

            if (CheckForDispose())
            {
                waveFile = new WaveFileWriter(WavFilePath, waveSource.WaveFormat);
            }
            waveSource.StartRecording();
            DiagBox.Text = "Recording....";
        }
        catch (System.UnauthorizedAccessException)
        {
            waveFile.Dispose();
            waveFile = null;
            DiagBox.Text = "Permissions Invalid";
            RecordingButton.Enabled = true;
            StopButton.Enabled = false;
        }

}

// Stop Button private void StopButton_Click(object sender, EventArgs e) { waveSource.StopRecording(); RecordingButton.Enabled = true; StopButton.Enabled = false; DiagBox.Text = "Please Press Recording";

        waveFile.Dispose();
        waveFile = null;

}

markheath commented 4 years ago

what does waveSource_DataAvailable do?

TheDJPHD commented 4 years ago

what does waveSource_DataAvailable do?

    void waveSource_DataAvailable(object sender, WaveInEventArgs e)
    {
        if (waveFile != null)
        {
            waveFile.Write(e.Buffer, 0, e.BytesRecorded);
            waveFile.Flush();
        }
    }
markheath commented 4 years ago

Can't see anything obviously wrong, although I'd recommend moving wavFile.Dispose into the recording stopped event handler. What happens if you explicitly pass device numbers into the WaveInEvent constructor?

Maybe the recording level has somehow been set to zero or that input has been muted on your computer.