2016-07-30 283 views
-1

当我尝试工作时,它显示在textviev“0.0”值上。 我得到的代码从这个网站主要是 我得到的权限 <uses-permission android:name="android.permission.RECORD_AUDIO" /> 我允许上设置 - 应用程序 - 麦克 它的返回值“0.0” 如何工作getAmplitude(); 可以有人解释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; 
} 

}

+0

我试图修复你的帖子,但它仍然是不可理解的。尝试[这](http://meta.stackoverflow.com/questions/291362/advice-for-non-native-english-speakers/291370#291370),它可能会有所帮助。 – peterh

回答

0

MediaRecorder documentationgetMaxAmplitude()描述:

返回,因为这种方法最后一次通话是被采样的最大绝对幅度。仅在setAudioSource()之后调用此方法。

返回:自上次调用测得的最大绝对幅度或0称为首次

这时候就是你得到0的第一次调用它在你的onCreate()

要求其定期,如果你想因为这是暗示你Runnable的存在,这里有一个例子调度您getAmplitudeEMA()每500ms:

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(); 
+0

İt的工作现在谢谢 –