在Windows手机中获取麦克风的振幅

时间:2014-04-08 21:18:18

标签: windows-phone-7 audio windows-phone-8

我想获得windows phone中麦克风输入的实时幅度。实现这一目标的最简单有效的方法是什么?

1 个答案:

答案 0 :(得分:1)

要获得振幅,您必须处理Microphone类的BufferReady事件:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/gg442302(v=vs.105).aspx

设置代码

Microphone microphone = Microphone.Default;
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
microphone.BufferDuration = TimeSpan.FromMilliseconds(1000);

byte[] buffer;
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];

microphone.Start();

事件处理程序块

void microphone_BufferReady(object sender, EventArgs e)
{

    microphone.GetData(buffer);


    for(int i = 0; i< buffer.Length; i+=2)
    {
        //The value of sample is the amplitude of the signal
        short sample = BitConverter.ToInt16(new byte[2] { buffer[i], buffer[i + 1] }, 0);
    }
}