這是我的代碼,我想運行它沒有按鈕如何更改切換按鈕使用void開始()
請幫助我;
我在Android的興趣,這是我對學校的期末項目
感謝出席
我的代碼
package com.andronoise;
import android.app.Activity;
import android.content.SharedPreferences;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
import java.text.DecimalFormat;
public class MainActivity extends Activity implements
InputListener {
InputSuara micInput;
TextView mdBTextView;
TextView mdBFractionTextView;
LevelBar mBarLevel;
double mOffsetdB = 10;
double mGain = 2500.0/Math.pow(10.0, 90.0/20.0);
double mRmsSmoothed;
double mAlpha = 0.9;
private int mSampleRate;
private int mAudioSource;
private volatile boolean mDrawing;
private volatile int mDrawingCollided;
private static final String TAG = "MainActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
micInput = new InputSuara(this);
setContentView(R.layout.activity_main);
mBarLevel = (LevelBar)findViewById(R.id.bar_level_drawable_view);
mdBTextView = (TextView)findViewById(R.id.dBTextView);
mdBFractionTextView = (TextView)findViewById(R.id.dBFractionTextView);
final ToggleButton onOffButton=(ToggleButton)findViewById(
R.id.on_off_toggle_button);
ToggleButton.OnClickListener tbListener =
new ToggleButton.OnClickListener() {
@Override
public void onClick(View v) {
if (onOffButton.isChecked()) {
readPreferences();
micInput.setSampleRate(mSampleRate);
micInput.setAudioSource(mAudioSource);
micInput.start();
} else {
micInput.stop();
}
}
};
onOffButton.setOnClickListener(tbListener);
}
private void readPreferences() {
SharedPreferences preferences = getSharedPreferences("LevelMeter",
MODE_PRIVATE);
mSampleRate = preferences.getInt("SampleRate", 8000);
mAudioSource = preferences.getInt("AudioSource",
MediaRecorder.AudioSource.VOICE_RECOGNITION);
}
@Override
public void processAudioFrame(short[] audioFrame) {
if (!mDrawing) {
mDrawing = true;
double rms = 0;
for (int i = 0; i < audioFrame.length; i++) {
rms += audioFrame[i]*audioFrame[i];
}
rms = Math.sqrt(rms/audioFrame.length);
mRmsSmoothed = mRmsSmoothed * mAlpha + (1 - mAlpha) * rms;
final double rmsdB = 20.0 * Math.log10(mGain * mRmsSmoothed);
mBarLevel.post(new Runnable() {
@Override
public void run() {
mBarLevel.setLevel((mOffsetdB + rmsdB)/100);
DecimalFormat df = new DecimalFormat("##");
mdBTextView.setText(df.format(20 + rmsdB));
int one_decimal = (int) (Math.round(Math.abs(rmsdB * 10))) % 10;
mdBFractionTextView.setText(Integer.toString(one_decimal));
mDrawing = false;
}
});
} else {
mDrawingCollided++;
Log.v(TAG, "Level bar update collision, i.e. update took longer " +
"than 20ms. Collision count" + Double.toString(mDrawingCollided));
}
}
}
哈哈哈同樣與你..你能解決我的問題代碼Ruchir? – Ginawan
恩......我甚至不知道你的問題是什麼!出了什麼問題?給我一些細節夥計! –
我想運行的應用程序沒有按開關按鈕..是的,它似乎沒有錯碼.. – Ginawan