本文介绍了如何吹MIC在Android设备时,得到的幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得吹进MIC在安卓设备时的幅度。

  MediaRecorder记录=新MediaRecorder();
   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

   定时器定时=新的Timer();
   timer.scheduleAtFixedRate(新RecorderTask(录像机),0,1000);

私有类RecorderTask扩展的TimerTask {
私人MediaRecorder记录;

公共RecorderTask(MediaRecorder记录器){
    this.recorder =记录;
}

公共无效的run(){
    Log.v(,幅度为+ recorder.getMaxAmplitude());
}
}
 

我得到一个错误:

  ERROR / AndroidRuntime(20927):java.lang.RuntimeException的:产生的原因setAudioSource失败。
ERROR / AndroidRuntime(20927):在android.media.MediaRecorder.setAudioSource(本机方法)
 

解决方案

 公共布尔isBlowing()
    {
        布尔记录= TRUE;

        INT minSize属性= AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT);
        AudioRecord AR =新AudioRecord(MediaRecorder.AudioSource.MIC,8000,AudioFormat.CHANNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,minSize属性);


        短[]缓冲区=新的短[minSize属性】;

        ar.startRecording();
        同时,(录音)
        {

            ar.read(缓冲液,0,minSize属性);
            对于(简称S:缓冲区)
            {
                如果(Math.abs(S)> 27000)// DETECT卷(如果我吹在MIC)
                {
                    blow_value = Math.abs(多个);
                    的System.out.println(吹值=+ blow_value);
                    ar.stop();
                    记录= FALSE;

                    返回true;

                }

            }
        }
        返回false;

    }
 

How to get the amplitude when blowing into MIC in android device.

MediaRecorder recorder = new MediaRecorder();
   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);

   Timer timer = new Timer();
   timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 1000);

private class RecorderTask extends TimerTask {
private MediaRecorder recorder;

public RecorderTask(MediaRecorder recorder) {
    this.recorder = recorder;
}

public void run() {
    Log.v("", "amplitude is" + recorder.getMaxAmplitude());
}
}

I am getting an error:

ERROR/AndroidRuntime(20927): Caused by: java.lang.RuntimeException: setAudioSource failed.
ERROR/AndroidRuntime(20927): at android.media.MediaRecorder.setAudioSource(Native Method)
解决方案
public boolean isBlowing()
    {
        boolean recorder=true;

        int minSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);
        AudioRecord ar = new AudioRecord(MediaRecorder.AudioSource.MIC, 8000,AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,minSize);


        short[] buffer = new short[minSize];

        ar.startRecording();
        while(recorder)
        {

            ar.read(buffer, 0, minSize);
            for (short s : buffer) 
            {
                if (Math.abs(s) > 27000)   //DETECT VOLUME (IF I BLOW IN THE MIC)
                {
                    blow_value=Math.abs(s);
                    System.out.println("Blow Value="+blow_value);
                    ar.stop();
                    recorder=false;

                    return true;

                }

            }
        }
        return false;

    }

这篇关于如何吹MIC在Android设备时,得到的幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:02