简体   繁体   English

在 SharpAvi 上将屏幕录制为视频、音频 - 音频未录制

[英]Screen recording as video, audio on SharpAvi - Audio not recording

Requirement:要求:

I am trying to capture Audio/Video of windows screen with SharpAPI Example with Loopback audio stream of NAudio Example .我正在尝试使用带有NAudio Example的环回音频流的SharpAPI Example捕获 Windows 屏幕的音频/视频。

I am using C#, wpf to achieve the same.我正在使用 C#、wpf 来实现相同的功能。

Couple of nuget packages.几个 nuget 包。 SharpAvi - forVideo capturing NAudio - for Audio capturing SharpAvi - 用于视频捕获NAudio - 用于音频捕获

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.我已经成功地将它与提供的示例集成在一起,我正在尝试通过NAudio和 SharpAPI 视频流捕获音频,以便将视频与音频实现一起录制。

Issue:问题:

Whatever I write the audio stream in SharpAvi video.无论我在 SharpAvi 视频中编写音频流。 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.但是当我尝试将音频捕获为名为“output.wav”的单独文件时,它按预期录制了音频,并且能够听到录制的音频。 So, I'm concluding for now that the issue is only on integration with video via SharpApi所以,我现在得出的结论是,问题仅在于通过 SharpApi 与视频集成

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

Full code to reproduce the issue:重现问题的完整代码:

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

Code glimpse from Recorder.cs来自 Recorder.cs 的代码概览

NAudio Initialization: NAudio 初始化:

audioSource = new WasapiLoopbackCapture();

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

audioSource.DataAvailable += audioSource_DataAvailable;

Capturing audio bytes and write it on SharpAvi Audio Stream:捕获音频字节并将其写入 SharpAvi 音频流:

    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.如果需要任何进一步的细节,请告诉我。

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.就我而言,问题是我每 0.1 秒获取一次音频并尝试同时编写新的视频和音频。 And getting new video data (taking screenshot) took me too long.获取新的视频数据(截屏)花了我太长时间。 Causing each frame was added every 0.3 seconds instead of 0.1.导致每帧每 0.3 秒而不是 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然后稍微优化代码到0.1秒以内,问题就解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM