6

大家晚上好!

我有以下麻烦:我正在尝试使用手机的麦克风测量(声音的)分贝,但不知道为什么它不起作用!!!有什么建议吗?谢谢你的帮助!!

程序是这样的:

`package com.dani;



import java.io.IOException;


import android.media.MediaRecorder;
import android.os.Bundle;
import android.app.Activity;

import android.widget.TextView;

public class Pruebita2 extends Activity {

TextView TextView;
StringBuilder builder=new StringBuilder();
MediaRecorder  mRecorder;
double powerDb;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pruebita2);
TextView=new TextView(this);
setContentView(TextView);

mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null"); 
try {
    mRecorder.prepare();
} catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
mRecorder.start();

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


}
 powerDb = 20 * log10(getAmplitude() / referenceAmp);//obtain the DECIBELS
}`
4

1 回答 1

8

这段代码对我有用:

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class Noise extends Activity {

TextView mStatusView;
MediaRecorder mRecorder;
Thread runner;
private static double mEMA = 0.0;
static final private double EMA_FILTER = 0.6;

final Runnable updater = new Runnable(){

    public void run(){          
        updateTv();
    };
};
final Handler mHandler = new Handler();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.noiselevel);
    mStatusView = (TextView) findViewById(R.id.status);


    if (runner == null)
    { 
        runner = new Thread(){
            public void run()
            {
                while (runner != null)
                {
                    try
                    {
                        Thread.sleep(1000);
                        Log.i("Noise", "Tock");
                    } catch (InterruptedException e) { };
                    mHandler.post(updater);
                }
            }
        };
        runner.start();
        Log.d("Noise", "start runner()");
    }
}

public void onResume()
{
    super.onResume();
    startRecorder();
}

public void onPause()
{
    super.onPause();
    stopRecorder();
}

public void startRecorder(){
    if (mRecorder == null)
    {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mRecorder.setOutputFile("/dev/null"); 
        try
        {           
            mRecorder.prepare();
        }catch (java.io.IOException ioe) {
            android.util.Log.e("[Monkey]", "IOException: " + 
android.util.Log.getStackTraceString(ioe));

        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " +   
android.util.Log.getStackTraceString(e));
        }
        try
        {           
            mRecorder.start();
        }catch (java.lang.SecurityException e) {
            android.util.Log.e("[Monkey]", "SecurityException: " +    
android.util.Log.getStackTraceString(e));
        }

        //mEMA = 0.0;
    }

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

public void updateTv(){
    mStatusView.setText(Double.toString((getAmplitudeEMA())) + " dB");
}
public double soundDb(double ampl){
    return  20 * Math.log10(getAmplitudeEMA() / ampl);
}
public double getAmplitude() {
    if (mRecorder != null)
        return  (mRecorder.getMaxAmplitude());
    else
        return 0;

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

}
于 2013-03-29T12:14:50.773 回答