我无法弄清楚如何在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();
任何帮助都将受到高度赞赏; - ; 如果你想通过这种方式听到声音的出现,我会把结果记录下来。
注意:如果我只是将声音录制到文件中并稍后打开它只是完美无缺,但如果我立即写入并播放,则会听到意外的声音......
答案 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();