windows phone 7 -如何从麦克风到扬声器流式传输音频



我希望将音频直接从麦克风流式传输到默认音频输出介质,无论是扬声器还是3.5mm插孔。

我发现播放音频的机制似乎取决于在SoundEffect中提供静态字节数来播放,如下所示:

SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
soundInstance = sound.CreateInstance();
soundIsPlaying = true;
soundInstance.Play();

我可以连续播放500毫秒或更短的声音效果来实现我的目标,但我想知道是否有一种更复杂的方法,让麦克风不断地写入内存流,音频播放控件可以不断地读取。

谢谢你的帮助!

我通过扩展MediaStreamSource类来解决这个问题,这个类使用麦克风向MediaElement播放器提供样本。

如果对这段代码有任何需求,我会很高兴把它贴在某个地方。现在,如果您想做同样的事情,这里有一个代码片段可以帮助您:

// 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));
}

当麦克风缓冲区返回时,你基本上只是报告一个样本已经准备好了。

在尝试MegaPhone项目时,我遇到了以下错误"对象引用未设置为对象的实例"。你能看一下吗?由于

在MS.Internal.XcpImports

。CheckHResult (UInt32人力资源)在MS.Internal.XcpImports。MediaElement MediaElement, Int32 streamIndex, StreamInteropWrapper streamWrapper, InternalStreamWrapper internalStream, Int64 offset, Int64 count, Int64 timein100 nanoseconds, Int64 durationin100 nanoseconds, Int32 attributeCount, Int32[] attributeLengths, StringBuilder属性)在System.Windows.Controls.MediaElement。MediaSourceReportGetSampleCompleted (MediaStreamSample MediaStreamSample)在System.Windows.Media.MediaStreamSource。ReportGetSampleCompleted (MediaStreamSample MediaStreamSample)在Megaphone.MicrophoneSource。microphone_BufferReady(对象发送者,EventArgs e)在Microsoft.Xna.Framework.Audio.Microphone。OnBufferReady (EventArgs args)在Microsoft.Xna.Framework.Audio.MicrophoneCollection。OnBufferReady (UInt32处理)在Microsoft.Xna.Framework.FrameworkDispatcher.Update ()在Megaphone.MainPage。dt_Tick(对象发送者,EventArgs e)

最新更新