安卓:从麦克风录音分贝 [英] Android: record decibel from microphone

查看:470
本文介绍了安卓:从麦克风录音分贝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了问题上实施的Andr​​oid这个功能... 我只需要输出麦克风redorded分贝,这是我无法理解的事情:

 公共类噪声扩展活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    MediaRecorder记录=新MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    定时器定时=新的Timer();
    timer.scheduleAtFixedRate(新RecorderTask(录像机),0,500);
}
私有类RecorderTask扩展的TimerTask {
    TextView的risultato =(TextView中)findViewById(R.id.risultato_recorder);
    私人MediaRecorder记录;
    公共RecorderTask(MediaRecorder记录器){
        this.recorder =记录;
    }
    公共无效的run(){
        risultato.setText(+ recorder.getMaxAmplitude());
    }
}
}
 

在TextView的,结果只印在第一时间,它的0,然后应用程序崩溃与: 11-29 14:43:27.133:E / AndroidRuntime(25785):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程

我已搜索周围,但我不能找到一个COM prehensive例如......只用了很多东西,类我不需要例子。 我可以解决这个问题?

解决方案

UI组件只能从UI线程修改。

你的任务是在后台线程中运行,因此你需要强制的TextView 更新要在UI线程中完成的。您可以使用 Activity.runOnUiThread 方法实现它。

试试这个:

 公共无效的run(){
    runOnUiThread(新的Runnable(){
        @覆盖
        公共无效的run(){
            risultato.setText(+ recorder.getMaxAmplitude());
        }
    });
}
 

而不是

 公共无效的run(){
    risultato.setText(+ recorder.getMaxAmplitude());
}
 

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?

解决方案

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());
}

这篇关于安卓:从麦克风录音分贝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆