我正在尝试播放另一个类在事件中返回的 PCM 音频样本。如果我将它们保存在一个文件中,然后将其作为流播放,一切正常。但在实时播放中没有任何反应。PlaybackState 设置为已停止。这是代码:
WaveFormat wf = new WaveFormat(8000, 1);
MemoryStream ms;
WaveStream blockAlignedStream;
WaveOut waveOut;
short[] PCMSamples = new short[320];
void StartPlayback()
{
ms = new MemoryStream();
blockAlignedStream = new BlockAlignReductionStream(WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.RawSourceWaveStream(ms[1], wf)));
waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback());
waveOut.Init(blockAlignedStream);
waveOut.Play();
}
public void PCMStreamEventHandler(byte[] data)
{
this.Invoke((MethodInvoker)delegate
{
var pos = ms.Position;
codec.Convert(data, PCMSamples);
ms.Position = ms.Length;
byte[] buffer = new byte[PCMSamples.Length * 2];
Buffer.BlockCopy(PCMSamples, 0, buffer, 0, buffer.Length);
ms.Write(buffer, 0, buffer.Length);
ms.Position = pos;
});
}
任何有关实时 PCM 播放的帮助将不胜感激。谢谢。