我希望将音频直接从麦克风流式传输到默认音频输出介质,无论是扬声器还是 3.5 毫米插孔。
我发现的播放音频的机制似乎取决于提供在 SoundEffect 中播放的静态字节数,如下所示:
SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
soundInstance = sound.CreateInstance();
soundIsPlaying = true;
soundInstance.Play();
我可以连续播放 500 毫秒或更短的 SoundEffects 来实现我的目标,但我想知道是否有一种更复杂的方法,其中涉及麦克风不断写入 MemoryStream,音频播放控件可以连续读取该内存流。
感谢您的帮助!
最佳答案
我通过使用麦克风向 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));
}
您基本上只是在返回麦克风缓冲区时报告样本已准备好。这样可以实现流畅的播放。
关于windows-phone-7 - 如何将音频从麦克风传输到扬声器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14405771/