0

I have a recorder created in the onCreate(), like final MediaRecorder recorder=new MediaRecorder();, and i need to stop recording when the user press the back button.

In order to stop the recorder i used the following code.

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

to intercept the back button, but it's outside the onCreate and i can't intercept the recorder!

Zak
  • 591
  • 2
  • 15
  • 37

4 Answers4

1

You can override onBackPressed instead:

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
       //Stop recorder
    }
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • if i put it in the onCreate it produce an error, if i put it outside, it don't recognise the recorder variable – Zak Nov 30 '12 at 12:02
  • 1
    @Zak Make it a global variable so you can access it here. This method should be outside the `onCreate` – IAmGroot Nov 30 '12 at 12:04
  • keep your recorder variable global. This method will be outside onCreate() only. You will have to declare variable before onCreate() method – MysticMagicϡ Nov 30 '12 at 12:04
  • thanks it works. but it doesn't stop the recorder (the imageview created by me stop it, so the code to stop is correct, but doesn't work here) – Zak Nov 30 '12 at 12:08
  • i am posting it in the main post, wait a moment – Zak Nov 30 '12 at 12:11
  • Ok. Try removing super.onBackPressed(); from method. Or add it as a last line of method – MysticMagicϡ Nov 30 '12 at 12:12
1

The default behaviour of onKeyDown, if the button is back, is to call onBackPressed. But since you have overridden the onKeyDown it might be that you only call onKeyDown. I don't know how it works when you have overridden both functions, but try to remove onKeyDown and only keep onBackPressed and see if the recorder.stop() gets called.

Zyber
  • 1,034
  • 8
  • 19
0

You have to use

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

This should work for you.

Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
0

You can always override any Key press. For overriding back button especially use

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

also for accessing recorder outside oncreate, declare MediaRecorder recorder as global variable, initialize it in onCreate() method

SKT
  • 1,821
  • 1
  • 20
  • 32