我正在尝试流式传输并播放来自 TCP:port 的音频数据。数据为无符号 8 位,数据速率为 11.025KHz。
我接收数据没有问题,但我需要一些代码帮助才能让音频流与 C# 和 NAudio 一起工作。
我正在尝试流式传输并播放来自 TCP:port 的音频数据。数据为无符号 8 位,数据速率为 11.025KHz。
我接收数据没有问题,但我需要一些代码帮助才能让音频流与 C# 和 NAudio 一起工作。
NAudio has BufferedWaveProvider class just for your purpose. You can use it like this:
var bufferedStream = new BufferedWaveProvider(format);
var waveOut = new WaveOut();
waveOut.Init(bufferedStream);
waveOut.Play();
And now you should supply buffered provider with your data (probably in another thread):
var buffer = GetData(); // Here is your code
bufferedStream.AddSamples(buffer, 0, buffer.Length);
Probably you'll want to check whether there is enough data in buffer, otherwise pause playback or something else.
This tutorial explains how it works in NAuduo. And here is an example.