Android: decibel recording from microphone

I have a problem with implementing this functionality in Android ... I only need to output the decibel edited from the microphone, and this is a thing I cannot understand:

public class Noise extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    MediaRecorder recorder=new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    Timer timer=new Timer();
    timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 500);
}
private class RecorderTask extends TimerTask{
    TextView risultato=(TextView) findViewById(R.id.risultato_recorder);
    private MediaRecorder recorder;
    public RecorderTask(MediaRecorder recorder){
        this.recorder = recorder;
    }
    public void run(){
        risultato.setText(""+recorder.getMaxAmplitude());
    }
}
}

In a text view, the result is printed only for the first time, and it is 0, and then the application crashes: 11-29 14: 43: 27.133: E / AndroidRuntime (25785): android.view.ViewRoot $ CalledFromWrongThreadException: only the source thread that created the view hierarchy may relate to his views.

I searched around, but I can’t find a comprehensive example ... only examples with lots of things and a class that I don't need. can i fix this problem?

0
source share
1 answer

.

, TextView . Activity.runOnUiThread.

:

public void run(){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            risultato.setText("" + recorder.getMaxAmplitude());
        }
    });
}

public void run(){
    risultato.setText(""+recorder.getMaxAmplitude());
}
+2

All Articles