6

Requirement:

I am trying to capture Audio/Video of windows screen with SharpAPI Example with Loopback audio stream of NAudio Example.

I am using C#, wpf to achieve the same.

Couple of nuget packages. SharpAvi - forVideo capturing NAudio - for Audio capturing

What has been achieved:

I have successfully integrated that with the sample provided and I'm trying to capture the audio through NAudio with SharpAPI video stream for the video to record along with audio implementation.

Issue:

Whatever I write the audio stream in SharpAvi video. On output, It was recorded only with video and audio is empty.

Checking audio alone to make sure:

But When I try capture the audio as separate file called "output.wav" and It was recorded with audio as expected and can able to hear the recorded audio. So, I'm concluding for now that the issue is only on integration with video via SharpApi

writterx = new WaveFileWriter("Out.wav", audioSource.WaveFormat);

Full code to reproduce the issue:

https://drive.google.com/open?id=1H7Ziy_yrs37hdpYriWRF-nuRmmFbsfe-

Code glimpse from Recorder.cs

NAudio Initialization:

audioSource = new WasapiLoopbackCapture();

audioStream = CreateAudioStream(audioSource.WaveFormat, encodeAudio, audioBitRate);

audioSource.DataAvailable += audioSource_DataAvailable;

Capturing audio bytes and write it on SharpAvi Audio Stream:

    private void audioSource_DataAvailable(object sender, WaveInEventArgs e)
    {
        var signalled = WaitHandle.WaitAny(new WaitHandle[] { videoFrameWritten, stopThread });
        if (signalled == 0)
        {
            audioStream.WriteBlock(e.Buffer, 0, e.BytesRecorded);               
            audioBlockWritten.Set();
            Debug.WriteLine("Bytes: " + e.BytesRecorded);
        }
    }

Can you please help me out on this. Any other way to reach my requirement also welcome.
Let me know if any further details needed.

Gopichandar
  • 2,742
  • 2
  • 24
  • 54
  • 1
    Please [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. – Progman Feb 15 '20 at 17:39
  • Can you add the code for `When I try capture the audio as separate file called "output.wav"` just so we can see where that's at? I'm not familiar with it, but looking at the documentation where does it begin recording into the video? – Jimmy Smith Feb 23 '20 at 16:03
  • Do you know if you are recording the correct audio source? – the Wongfon Semicolon Feb 24 '20 at 20:04
  • @JimmySmith Thank for you time. You can found both Video and separate audio in `Bin\Debug\`folder (executable location). – Gopichandar Feb 25 '20 at 03:41
  • @theWongfonSemicolon Yup, it was `WasapiLoopbackCapture`to capture the loopback audio through naudio. – Gopichandar Feb 25 '20 at 03:42
  • Were you able to solve this problem? – skv Nov 06 '21 at 09:44

1 Answers1

0

Obviously, author doesn't need it, but since I run to the same problem others might need it.

Problem in my case was that I was getting audio every 0.1 seconds and attempted to write both new video and audio at the same time. And getting new video data (taking screenshot) took me too long. Causing each frame was added every 0.3 seconds instead of 0.1. And that caused some problems with audio stream being not sync with video and not being played properly by video players (or whatever it was). And after optimizing code a little bit to be within 0.1 second, the problem is gone

skv
  • 85
  • 9