Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid on back key pressed stop recorder
    primarykey
    data
    text
    <p>I have a recorder created in the <code>onCreate()</code>, like final <code>MediaRecorder recorder=new MediaRecorder();</code>, and i need to stop recording when the user press the back button.</p> <p>In order to stop the recorder i used the following code.</p> <pre><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())&lt;(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)); } }); } } </code></pre> <p>to intercept the back button, but it's outside the onCreate and i can't intercept the recorder!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload