3

I'm using CSCore.WasapiLoopbackCapture to record system sound. But when there is no sound in system, it doesn't record at all! For instance, while playing music and recording it, the output file's duration is less than the track's. I want it to continue recording even when there is no sound in the system but I didn't find any property to control this behavior. Here is my code snippet:

WasapiCapture waveLoop = new WasapiLoopbackCapture();
waveLoop.Initialize();
waveLoop.DataAvailable += waveLoop_DataAvailable;
waveLoop.Stopped += waveLoop_Stopped;
waveLoop.Start();
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SepehrM
  • 1,087
  • 2
  • 20
  • 44

1 Answers1

5

There is no "nice" solution for your "problem". But your problem already got described here:

Another oddity is that WASAPI will only push data down to the render endpoint when there are active streams. When nothing is playing, there is nothing to capture.

That means that there is nothing you can do against that behavior. If there is nothing playing, nothing will be captured. The easiest solution is:

But my particular favorite way of handling this is to run silence.exe. That way there are never any "nothing is playing" glitches, because there's always something playing.

So just make sure your application or any other application plays silence. That would be a way to make sure that your application records silence instead of interrupting the capture.

Florian
  • 5,918
  • 3
  • 47
  • 86
  • Thanks. And where should I find this silence.exe? Can't it be achieved in .net without running another assembly? – SepehrM Jun 10 '14 at 14:05
  • Of course it can. For example, you can achieve it with CSCore. – Florian Jun 10 '14 at 14:06
  • Could you give me some hints? I'm pretty new to CSCore! – SepehrM Jun 10 '14 at 14:11
  • 1
    Well. You basically just need to implement a new WaveSource. E.g. something like this: http://pastebin.com/9MYM3yFN. After that, just follow this tutorial: https://cscore.codeplex.com/wikipage?title=Play%20a%20sound&referringTitle=Documentation. Replace `return CodecFactory.Instance.GetCodec(@"C:\Temp\sound.mp3");` by `new SilenceGenerator()`. That's all. – Florian Jun 10 '14 at 14:14
  • And how can I distinguish this silence? When recording I'm showing a wave form to inform the user. How can I check if there is only silence, in DataAvailable event? – SepehrM Jun 11 '14 at 18:37
  • Well. That is a bit too complex to answer in a single comment. Better ask a new question. – Florian Jun 11 '14 at 21:02
  • 1
    Hey! thank you thefiloe, just to add that in your pastebin sample, we must replace line 13: "get { throw new NotImplementedException(); }" by "get { return _waveFormat; }". It's pretty easy to figure out but not that obvious at first glance – fredericrous Nov 16 '14 at 12:57