我通过使用麦克风向 MediaElement Player 提供样本的类扩展 MediaStreamSource 类来解决这个问题。
如果对这段代码有任何需求,我会很乐意将其发布到某个地方。现在,如果您尝试做同样的事情,这里有一个代码片段可以帮助您:
// Provides audio samples from AudioSampleProvider property.
// (MediaStreamType parameter will always equal Audio.)
protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
// start the microphone capture if it's not started yet
if (microphone.State == MicrophoneState.Stopped)
{
microphone.Start();
}
}
// gets called back when the microphone's buffer is ready
private void microphone_BufferReady(object sender, EventArgs e)
{
// Retrieve audio data
microphone.GetData(buffer);
// Reset MemoryStream object
memoryStream.Seek(0, SeekOrigin.Begin);
// Write the newly acquired data into the memory stream
memoryStream.Write(buffer, 0, buffer.Length);
// Send out the sample
ReportGetSampleCompleted(new MediaStreamSample(mediaStreamDescription,
memoryStream,
0,
buffer.Length,
0,
mediaSampleAttributes));
}
基本上,只要返回麦克风缓冲区,您就可以报告样本已准备就绪。这导致流畅的播放。