安卓在后退键按下停止记录器



我在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 之外,我无法拦截录音机!

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

你可以改用 onBackPressed 覆盖:

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

你必须使用

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

这应该适合您。

您始终可以覆盖任何按键。对于覆盖后退按钮,特别是使用

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

同样为了在oncreate之外访问记录器,将MediaRecorder recorder声明为全局变量,并在onCreate()方法中初始化它

相关内容

  • 没有找到相关文章

最新更新