c#cscore如何即时播放环回捕获记录的字节

时间:2017-07-06 19:36:06

标签: c# playback cscore

哟伙计们,我的再次提出我的菜鸟问题。所以这次我用cscore录制了窗口声音,然后通过套接字将录制的字节发送到另一台电脑,然后让它们在那里播放。

我无法弄清楚如何在DataAvailable回调下播放获得的字节...

我已经尝试将获得的字节写入文件并播放该文件但该声音无法正常播放,因为它也听到了一些意想不到的声音。

所以这是我的代码:

WasapiCapture capture = new WasapiLoopbackCapture();
capture.Initialize();
capture.DataAvailable += (s, e) =>
{
    WaveWriter w = new WaveWriter("file.mp3", capture.WaveFormat);
    w.Write(e.Data, e.Offset, e.ByteCount);
    w.Dispose();
    MemoryStream stream = new MemoryStream(File.ReadAllBytes("file.mp3"));
    SoundPlayer player = new SoundPlayer(stream);
    player.Play();
    stream.Dispose();
};
capture.Start();

任何帮助都将受到高度赞赏; - ; 如果你想通过这种方式听到声音的出现,我会把结果记录下来。

注意:如果我只是将声音录制到文件中并稍后打开它只是完美无缺,但如果我立即写入并播放,则会听到意外的声音......

1 个答案:

答案 0 :(得分:0)

使用SoundInSource作为适配器。

var capture = new WasapiCapture(...)
capture.Initialize(); //initialize always first!!!!

var soundInSource = new SoundInSource(capture)
    { FillWithZeros = true }; //set FillWithZeros to true, to prevent WasapiOut from stopping for the case WasapiCapture does not serve any data

var soundOut = new WasapiOut();
soundOut.Initialize(soundInSource);
soundOut.Play();