如何從用戶語音中檢測振幅? 只需檢測用戶是否說話。 AudioRecorder,MediaRecorder,Audio Capture有什麼區別? 請給我一個充分的解釋和代碼,請從用戶語音中獲取振幅
我試圖從這個教程類,但它的主要錯誤在「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;
}
}
更新我的第二次嘗試
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;
}}
這就是我的代碼,但它不工作 – 2013-02-21 05:52:47