Android on back键按下停止记录器

时间:2012-11-30 11:57:22

标签: android back onkeydown

我在onCreate()创建了一个录音机,如最终MediaRecorder recorder=new MediaRecorder();,我需要在用户按下后退按钮时停止录音。

为了停止录像机,我使用了以下代码。

final MediaRecorder recorder=new MediaRecorder();
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.noise);
    // elementi
        TextView picco=(TextView) findViewById(R.id.picco_recorder);
        ImageView noise_bar=(ImageView) findViewById(R.id.noise_bar);
        ImageView noise_bar_red=(ImageView) findViewById(R.id.noise_bar_red);
        ImageView imgnoise = (ImageView) findViewById(R.id.logo_noise);
        imgnoise.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                recorder.stop();
                recorder.release();
                Intent torna_home=new Intent(Noise.this,Index.class);
                startActivity(torna_home);
            }
        });
    // noise
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile("/dev/null");
        try{
            recorder.prepare();
        }catch(IllegalStateException e){
            Log.d("Errore",e.toString());
            e.printStackTrace();
        }catch(IOException e){
            Log.d("Errore",e.toString());
            e.printStackTrace();
        }
        recorder.start();
        double db=10*Math.log(recorder.getMaxAmplitude());
        picco.setText("" + (int)db);
        noise_bar.scrollTo(0, (int)((int)db*1.5));
        noise_bar_red.scrollTo(0, (int)((int)db*1.5));
        Timer timer=new Timer();
        timer.scheduleAtFixedRate(new RecorderTask(recorder), 0, 500);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Intent torna_home=new Intent(Noise.this,Index.class);
        startActivity(torna_home);
    }
    return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
    super.onBackPressed();
    recorder.stop();
    recorder.release();
}
private class RecorderTask extends TimerTask{
    TextView risultato=(TextView) findViewById(R.id.picco_recorder);
    TextView max=(TextView) findViewById(R.id.max_recorder);
    TextView media=(TextView) findViewById(R.id.media_recorder);
    ImageView noise_bar=(ImageView) findViewById(R.id.noise_bar);
    ImageView noise_bar_red=(ImageView) findViewById(R.id.noise_bar_red);
    private MediaRecorder recorder;
    public RecorderTask(MediaRecorder recorder){
        this.recorder = recorder;
    }
    public void run(){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                double db=10*Math.log(recorder.getMaxAmplitude());
                risultato.setText("" + (int)db);
                if(Integer.parseInt(max.getText().toString())<(int)db){
                    max.setText("" + (int)db);
                    noise_bar_red.scrollTo(0, (int)((int)db*1.5));
                }
                media.setText(""+((int)db+Integer.parseInt(max.getText().toString()))/2);
                noise_bar.scrollTo(0, (int)((int)db*1.5));
            }
        });
    }
}

拦截后退按钮,但它在onCreate之外,我无法拦截录音机!

4 个答案:

答案 0 :(得分:1)

您可以改为覆盖onBackPressed:

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
       //Stop recorder
    }

答案 1 :(得分:1)

如果按钮返回,onKeyDown的默认行为是调用onBackPressed。但是,由于您已覆盖onKeyDown,因此您可能只调用onKeyDown。当你覆盖这两个函数时,我不知道它是如何工作的,但是尝试删除onKeyDown并且只保留onBackPressed并查看recorder.stop()是否被调用。

答案 2 :(得分:0)

你必须使用

@Override
public void onBackPressed() {
    super.onBackPressed()
//Add your desired code here
}

这应该适合你。

答案 3 :(得分:0)

您可以随时覆盖任何按键。 对于覆盖后退按钮,尤其是使用

@Override
public void onBackPressed() {
   super.onBackPressed();
   recorder.stop();//or whatever code you use to stop recorder
}

也可以在oncreate外部访问记录器,将MediaRecorder recorder声明为全局变量,在onCreate()方法中初始化