首页 新闻 会员 周边 捐助

windowsphone8如何获取录音时麦克风的音量?

0
[已解决问题] 解决于 2013-09-12 09:41

windowsphone8如何获取录音时麦克风的音量,来产生录音音量动画的效果?

灬番茄的主页 灬番茄 | 菜鸟二级 | 园豆:204
提问于:2013-08-27 15:18
< >
分享
最佳答案
0

代码:

public class Decibelmeter
    {
        public event EventHandler Update = null;

        private bool _isStart = false;
        private int _decibelValue = 0;
        private byte[] _buffer = null;

        public bool IsStart
        {
            get { return _isStart; }
        }

        public int DecibelValue
        {
            get { return _decibelValue; }
        }

        public Decibelmeter()
        {
            Microphone.Default.BufferDuration = TimeSpan.FromSeconds(0.1d);

            int sampleSizeInBytes = Microphone.Default.GetSampleSizeInBytes(Microphone.Default.BufferDuration);
            this._buffer = new byte[sampleSizeInBytes];

            CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
            FrameworkDispatcher.Update();
        }

        void CompositionTarget_Rendering(object sender, EventArgs e)
        {
            FrameworkDispatcher.Update();
        }

        public void Start()
        {
            Microphone.Default.BufferReady += new EventHandler<EventArgs>(Default_BufferReady);
            Microphone.Default.Start();
        }

        public void Stop()
        {
            Microphone.Default.BufferReady -= new EventHandler<EventArgs>(Default_BufferReady);
            Microphone.Default.Stop();
        }

        void Default_BufferReady(object sender, EventArgs e)
        {
            int data = Microphone.Default.GetData(this._buffer);
            if (data == 0)
            {
                return;
            }

            int averageVolume = this.GetAverageVolume(data);
            this._decibelValue = (int)((long)averageVolume / 200L);

            this.OnUpdate(EventArgs.Empty);
        }

        private int GetAverageVolume(int data)
        {
            long number = 0L;
            for (int i = 0; i < data; i += 2)
            {
                int number2 = Math.Abs((int)BitConverter.ToInt16(this._buffer, i));
                number += (long)number2;
            }
            return (int)(number / (long)(data / 2));
        }

        protected virtual void OnUpdate(EventArgs e)
        {
            if (this.Update != null)
            {
                this.Update(this, e);
            }
        }
    }

 

 

使用:

//  类全局变量
private Decibelmeter _decibelmeter = null;

// 初始化Decibelmeter 
this._decibelmeter = new Decibelmeter();
            this._decibelmeter.Update += new EventHandler(_decibelmeter_Update);
            this._decibelmeter.Start();


// Decibelmeter 更新
void _decibelmeter_Update(object sender, EventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(() =>
            {
// 这里得到的value就是话筒录音的大小量
                var value = this._decibelmeter.DecibelValue;

                
            }));
        }
奖励园豆:5
~冻结~ | 初学一级 |园豆:39 | 2013-09-10 18:19

非常感谢~!

灬番茄 | 园豆:204 (菜鸟二级) | 2013-09-11 11:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册