Screen recording as video, audio on SharpAvi - Audio not recording
Asked Answered
D

1

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.

Doubtless answered 15/2, 2020 at 11:51 Comment(6)
Please edit your question to include the source code you have as a minimal reproducible example, which can be compiled and tested by others.Holliman
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?Teetotalism
Do you know if you are recording the correct audio source?Cheju
@JimmySmith Thank for you time. You can found both Video and separate audio in `Bin\Debug`folder (executable location).Doubtless
@theWongfonSemicolon Yup, it was WasapiLoopbackCaptureto capture the loopback audio through naudio.Doubtless
Were you able to solve this problem?Somme
S
1

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

Somme answered 6/11, 2021 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.