-1

当我尝试工作时,它会显示在 textviev "0.0" 值上。我从这个网站获得代码主要是我获得了 <uses-permission android:name="android.permission.RECORD_AUDIO" /> ı 授予设置应用程序麦克风的权限它的返回值“0.0”如何真正工作 getAmplitude(); 有人能解释一下吗

static final private double EMA_FILTER = 0.6;
TextView text;
Button buton;
Handler handler;
boolean state = false;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_karpuz_sec);
    text = (TextView) findViewById(R.id.text);
    buton = (Button) findViewById(R.id.button);
    handler = new Handler();
    buton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!state){
                try {
                    start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else
                stop();
        }
    });
}
Runnable runnable = new Runnable() {
    @Override
    public void run() {
        while (!state){
            text.setText(Double.toString(getAmplitudeEMA()));;
            handler.postDelayed(this,100);
        }
    }
};

private MediaRecorder mRecorder = null;
private double mEMA = 0.0;

public void start() throws IOException {
    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");
        mRecorder.prepare();
        mRecorder.start();
        mEMA = 0.0;
        state = true;
        Toast.makeText(getApplicationContext(),"Ses Ölçümü Başladı",Toast.LENGTH_SHORT).show();
        buton.setText("DURDUR");
        text.setText(Double.toString(getAmplitudeEMA()));

    }
}

public void stop() {
    if (mRecorder != null) {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;
        state = false;
        Toast.makeText(getApplicationContext(),"Ses Ölçümü Durdu",Toast.LENGTH_SHORT).show();
        buton.setText("BAŞLAT");
    }
}

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

}

4

1 回答 1

0

MediaRecorder 文档中,getMaxAmplitude()描述:

返回自上次调用此方法以来采样的最大绝对幅度。仅在 setAudioSource() 之后调用它。

返回:自上次调用以来测量的最大绝对幅度,或第一次调用时为 0

这就是为什么你第一次在你的onCreate().

如果您愿意,请定期请求它,因为这表明您的存在,这是一个每 500 毫秒Runnable安排一次的示例:getAmplitudeEMA()

private Timer mTimer = new Timer();

.....

mTimer.scheduleAtFixedRate(new TimerTask() {

    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                text.setText(Double.toString(getAmplitudeEMA()));
            }
        });
    }

}, 0, 500);

stop()/中onPause()

mTimer.cancel();
mTimer.purge();
于 2016-07-30T01:17:58.147 回答