2013-03-28 30 views
2

給大家晚上好!用手機測量分貝

我有以下麻煩:我試着測量使用我的手機麥克風分貝(的聲音),但不知道爲什麼它不工作!任何建議??謝謝你的幫助!

程序是這樣的:

`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 
}` 
+0

[測量分貝]的可能重複(http://stackoverflow.com/questions/15690116/measuring-分貝)。請不要轉發。 –

+0

我想從我的手機麥克風得到分貝,我發現該程序如果你知道另一個程序更好,我會很高興!! ahahahahha –

+1

好的,對不起,我不知道我在這裏新的 –

回答

5

此代碼的工作對我來說:

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; 
} 

} 
+1

此代碼適用於me.just一些提示...不要忘記添加 to android manifest –