0

我试图从它检测到的每次拍手中获取振幅读数,但每次检测到拍手时,我都会不断IllegalStateException0getMaxAmplitude(). 我做错什么了吗?有没有解决的办法?请告诉我!

@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();
}
4

1 回答 1

0

检查javadoc 的 MediaRecorder#getMaxAmplitude 方法
它说 setAudioSource 方法应该在之前被调用。
请注意本 javadoc 开头的状态和用法示例。getMaxAmplitude 只是一个吸气剂,它不执行实际记录。

于 2019-11-20T05:49:14.493 回答