Code example for MediaRecorder

Methods: getMaxAmplitudereleasestop

1
			e.printStackTrace();
		} 
	} 
	public void pause () { 
		timer.cancel();
		recorder.stop();
		recorder.release();
	} 
	public void resume () { 
		this.start();
	} 
	public void stop() { 
		 
	} 
	 
	private void startTimer () { 
		timer.scheduleAtFixedRate(new RecorderTask(), INTERVAL, INTERVAL);
	} 
	private void resetMediaRecorder() 
	{ 
		recorder.stop();
		recordCount = 0;
	 
		try 
		{ 
			recorder = getRecorder();
			recorder.prepare();
			recorder.start();
		} catch (IllegalStateException e)
		{ 
			e.printStackTrace();
		} catch (IOException e)
		{ 
			e.printStackTrace();
		} 
	} 
	public void setListener(SoundMonitorListener listener)
	{ 
		this.listener = listener;
	} 
	MediaRecorder getRecorder()
	{ 
		MediaRecorder recorder = new MediaRecorder();
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		Uri uri = Uri.fromFile(new File(TMP_SOUND_PATH));
		recorder.setOutputFile(uri.getPath());
		return recorder;
	} 
 
	public interface SoundMonitorListener { 
		void displayMaxAmplitude(int maxAmplitude);
		void onLargeSoundDetected(); 
		 
	} 
 
	class RecorderTask extends TimerTask {
	 
		public void run() { 
			handler.post(new Runnable(){
	 
				@Override 
				public void run() 
				{ 
					int maxAmplitude = recorder.getMaxAmplitude();
					listener.displayMaxAmplitude(maxAmplitude);
					if (maxAmplitude > LARGE_SOUND_THRESHOLD)
					{ 
						pause(); 
						System.out.println("Detected large sound.");