-2

How to detect amplitude from user's voice? Just detect if user speak or not. What is the different between AudioRecorder, MediaRecorder, Audio Capture? Please give me a full explanation and code please

i tried this class from tutorial but it has error in "mRecorder.prepare():"

import android.media.MediaRecorder;

 public class SoundMeter {
    static final private double EMA_FILTER = 0.6;

    private MediaRecorder mRecorder = null;
    private double mEMA = 0.0;

    public void start() {
            if (mRecorder == null) {
                    mRecorder = new MediaRecorder();
                    mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    mRecorder.prepare();
                mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                mRecorder.setOutputFile("/dev/null"); 

                mRecorder.start();
                mEMA = 0.0;
            }
    }

    public void stop() {
            if (mRecorder != null) {
                    mRecorder.stop();       
                    mRecorder.release();
                    mRecorder = null;
            }
    }

    public double getAmplitude() {
            if (mRecorder != null)
                    return  (mRecorder.getMaxAmplitude()/2700.0);
            else
                    return 0;

    }

    public double getAmplitudeEMA() {
            double amp = getAmplitude();
            mEMA = EMA_FILTER * amp + (1.0 - EMA_FILTER) * mEMA;
            return mEMA;
    }

}

update my second try

     public class MainActivity extends Activity {


 static final private double EMA_FILTER = 0.6;
private MediaRecorder mRecorder = null;
private double mEMA = 0.0;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


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

      mRecorder.start();
      mEMA = 0.0;


     tv = (TextView) findViewById(R.id.textView1);
    double a = getAmplitude();
    tv.setText(String.valueOf(a));
    new Timer().schedule(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            down();
        }
    }, 500, 10);

}
public void down() {
    this.runOnUiThread(Update);
}
private Runnable Update=new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        double a = getAmplitude();
        tv.setText(String.valueOf(a));
    }
};


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
public double getAmplitude() {
       if (mRecorder != null)
               return  (mRecorder.getMaxAmplitude());
       else
               return 0;

} }

Alvin Christian
  • 153
  • 1
  • 1
  • 8

1 Answers1

1

A couple of things:

  1. You need to set Output Format before the prepare() statement. From the documentation for prepare(): Throws IllegalStateException if it is called after start() or before setOutputFormat()

  2. MediaRecorder.prepare() also throws IOException. You may want to surround the prepare() with a try/catch construct.

try:

if (mRecorder == null) {
    try {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile("/dev/null"); 

        mRecorder.prepare();
        mRecorder.start();
        mEMA = 0.0;
    }
    catch (IOException e) {
        //do something with e
    }
}
mcalex
  • 6,628
  • 5
  • 50
  • 80
  • i have an error while debug this, "Source not found", why? – Alvin Christian Feb 21 '13 at 09:21
  • not sure. which line? (silly question) do you have a mic attached? – mcalex Feb 21 '13 at 09:24
  • mic? i debug it on my Android device. i just need to detect user's speak or not, still i have to set audio sorce, format, encoder? – Alvin Christian Feb 21 '13 at 09:28
  • oops lol, of course mic attached. You've already set source (mic) format (3GPP) and encoder (amr narrow band). Also, if you set output file to /dev/null you won't get a recording – mcalex Feb 21 '13 at 09:40
  • i see in other thread that if i just need to detect sound i dont need to set format and encoder and encoder. Btw if i not use try catch and just declare the Mediaplayer i get error source not found, if i use it and try to get amplitude, th result is always 0.0 Help me Take a look, i updated my answer – Alvin Christian Feb 21 '13 at 09:48