2015-12-10 59 views
-2

這是我的代碼,我想運行它沒有按鈕如何更改切換按鈕使用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)); 
    } 
    } 
} 
+0

哈哈哈同樣與你..你能解決我的問題代碼Ruchir? – Ginawan

+0

恩......我甚至不知道你的問題是什麼!出了什麼問題?給我一些細節夥計! –

+0

我想運行的應用程序沒有按開關按鈕..是的,它似乎沒有錯碼.. – Ginawan

回答

0

你想要做一些代碼,當用戶點擊任何地方。

好吧......我想我終於明白你在說什麼了。每當用戶點擊隨時隨地時間,這種方法將被調用:

@Override 
    public void onUserInteraction() { 
     super.onUserInteraction(); 
}  
你的情況

所以,代碼應該是:

 @Override 
     public void onUserInteraction() { 
      super.onUserInteraction(); 
if (onOffButton.isChecked()) { 
      readPreferences(); 
      micInput.setSampleRate(mSampleRate); 
      micInput.setAudioSource(mAudioSource); 
      micInput.start(); 
     } else { 
      micInput.stop(); 
     } 

編輯:

從文檔:

enter image description here

我不確定何時需要執行代碼,但看看圖表並找到合適的位置。這裏有一個例子:

@Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    if (onOffButton.isChecked()) { 
        readPreferences(); 
        micInput.setSampleRate(mSampleRate); 
        micInput.setAudioSource(mAudioSource); 
        micInput.start(); 
       } else { 
        micInput.stop(); 
    } 
0

像這樣

@Override 
    public void onResume() { 
      super.onResume(); 

      Inisialisasi(); 
      mDisplay.setLevel(0, mThreshold); 

      if (!mRunning) { 
       mRunning = true; 
       start(); 
      } 
    } 

    @Override 
    public void onStop() { 
      super.onStop(); 
      stop(); 

    } 

    private void start() { 

      mSensor.start(); 
      if (!mWakeLock.isHeld()) { 
        mWakeLock.acquire(); 
      } 

      mHandler.postDelayed(mPollTask, POLL_INTERVAL); 
    } 
+0

或我會給你我所有的源代碼..仍然不工作Ruchir ..怎麼樣?請.. – Ginawan