我试图从它检测到的每个拍子中获取振幅读数,但是每次它检测到一个拍子时,我总是从IllegalStateException
中获取0
或getMaxAmplitude()
。我做错什么了吗?有没有解决的办法?请告诉我!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Checks Permission
checkPermission(Manifest.permission.RECORD_AUDIO,REQUEST_RECORD_AUDIO_PERMISSION);
double threshold = 8;
double sensitivity = 45;
PercussionOnsetDetector mPercussionDetector = new PercussionOnsetDetector(22050, 1024,
new OnsetHandler() {
@Override
public void handleOnset(double time, double salience) {
/* try {
new MediaRecorder().setAudioSource(MediaRecorder.AudioSource.MIC);
new MediaRecorder().setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
new MediaRecorder().setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
new MediaRecorder().setOutputFile("dev/null");
new MediaRecorder().prepare();
new MediaRecorder().start();
} catch (Exception e) {
e.printStackTrace();
} */
runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println(new MediaRecorder().getMaxAmplitude());
System.out.println("YAAAYYY");
}
});
}
}, sensitivity, threshold);
dispatcher.addAudioProcessor(mPercussionDetector);
new Thread(dispatcher,"Audio Dispatcher").start();
}
最佳答案
检查javadoc for MediaRecorder#getMaxAmplitude method
它说setAudioSource方法应该在之前被调用。
请注意此javadoc开头的状态和用法示例。 getMaxAmplitude只是一个 setter/getter ,它不会执行实际的记录。