0

i've got problem on implementing this functionality in Android... i need only to output the decibel redorded from the microphone, and it's a thing that i can't 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 the textview, the result is printed the first time only, and it's 0, and then the app crash with: 11-29 14:43:27.133: E/AndroidRuntime(25785): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

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

Zak
  • 591
  • 2
  • 15
  • 37

1 Answers1

2

UI components can only be modified from the UI thread.

Your task is running in a background thread, so you need to force the TextView update to be done in the UI thread. You can achieve it with the Activity.runOnUiThread method.

Try this:

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

instead of

public void run(){
    risultato.setText(""+recorder.getMaxAmplitude());
}
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • thanks, i've understanded this thing. but now it give me new errors: https://dl.dropbox.com/u/16047047/log.txt logcat is not so comprehensive... – Zak Nov 29 '12 at 14:00
  • what is on line 29 of Noise.java ? – sdabet Nov 29 '12 at 14:02
  • the line risultato you wrote to me – Zak Nov 29 '12 at 14:04
  • What is the name of your layout XML file ? You need to load it in the onCreate method of your activity with `setContentView` – sdabet Nov 29 '12 at 14:06
  • nooo unbelivable! i've missed that :-D ops ahahah thanks, it work. now i need to know why it always return 0, also if i scream on the mic :-| – Zak Nov 29 '12 at 14:10
  • sure. you have any idea for the 0? – Zak Nov 29 '12 at 14:22
  • That may be helpful: http://stackoverflow.com/questions/6600364/android-mediarecorder-getmaxamplitude-always-returns-0-on-lg-optimus – sdabet Nov 29 '12 at 14:26
  • mpf... this part it's difficult. i can't understand. it crash saying start is called in an invalid state 4 – Zak Nov 29 '12 at 14:42
  • read this: http://developer.android.com/reference/android/media/MediaRecorder.html – sdabet Nov 29 '12 at 14:45