Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:32:03+00:00 2026-06-15T05:32:03+00:00

I have a recorder created in the onCreate() , like final MediaRecorder recorder=new MediaRecorder();

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:32:05+00:00Added an answer on June 15, 2026 at 5:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an application in javascript where a user clicks a button to
I have created a voice recorder applet. The output sound file will be .wav
I have to following code to launch and the audio recorder on Android: final
If I have two trivial stored procedures, created like so: create procedure root as
I have created the following progamm with a really simple mission: recording and then
I have the following array, I need to reorder it by average rating descending.
I'm developing a recorder in silverlight and I need to upload data from stream
I have control something like this #import <Foundation/Foundation.h> #import AQLevelMeter.h #import AQPlayer.h #import AQRecorder.h
I Have created an app that records and plays sound and I am looking
I am creating a video recorder and would like to create video player to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.