0

This code will generate a wav file but does not contain any recording

// Define the output wav file of the recorded audio string outputFilePath = @"C:\Users\system_recorded_audio.wav";

        // Redefine the capturer instance with a new instance of the LoopbackCapture class
        WasapiCapture CaptureInstance = new WasapiLoopbackCapture();

        // Redefine the audio writer instance with the given configuration
        WaveFileWriter RecordedAudioWriter = new WaveFileWriter(outputFilePath, CaptureInstance.WaveFormat);

        // When the capturer receives audio, start writing the buffer into the mentioned file
        CaptureInstance.DataAvailable += (s, a) =>
        {
            DelaySeconds(5);
            // Write buffer into the file of the writer instance
            RecordedAudioWriter.Write(a.Buffer, 0, a.BytesRecorded);
        };
            DelaySeconds(5);
        // When the Capturer Stops, dispose instances of the capturer and writer
        CaptureInstance.RecordingStopped += (s, a) =>
        {
            RecordedAudioWriter.Dispose();
            RecordedAudioWriter = null;
            CaptureInstance.Dispose();
        };

        // Start audio recording !
        CaptureInstance.StartRecording();
    }
    public static void DelaySeconds(int iTimeSec)
    {
        TimeSpan delay = TimeSpan.FromSeconds(iTimeSec);
        DateTime start = DateTime.Now;
        TimeSpan elapsed = TimeSpan.Zero;

        // waits in a while loop while checking the abort or cancel flag
        while (elapsed < delay)
        {
            Thread.Sleep(10);
            elapsed = DateTime.Now - start;
        }
    }
sameer
  • 9
  • 2
  • Consider calling `Flush` on `RecordedAudioWriter`. – mjwills May 01 '20 at 00:30
  • Did you forget to call `StopRecording`? – mjwills May 01 '20 at 00:32
  • Thanks for your feedback, I've tried both but still there is no recording inside the file. Is there a way to define which device to record from as I have multiple audio device connected to PC? – sameer May 01 '20 at 15:36
  • Can you update the question to reflect your current code? – mjwills May 02 '20 at 08:37
  • You need to get rid of `DelaySeconds` from inside the `DataAvailable` event handler – Mark Heath May 09 '20 at 09:36
  • Thank you Mark, now I'm trying to select recording device using "var recordingDevice = new WasapiIn(captureDevice, ...);" But seems like WasapiIn option has been removed. What other way to select capture device? – sameer May 10 '20 at 17:18

0 Answers0