Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /****
- * Method to start the record of the patient, for the current exercise.
- * Synchornized method because of the thread which is send a message every 2 seconds to detect
- * a sound presence or not.
- * If not : call to stopRecord method
- * @param path
- */
- public synchronized void beginRecord(String path){
- Log.i(TAG, "beginRecord" );
- if (!recorderRunning){
- recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
- recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
- recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
- recorder.setOutputFile(path);
- try {
- recorder.prepare();
- } catch (IllegalStateException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- recorder.start(); // Recording is now started
- recorderRunning = true;
- }
- }
- /***
- * Method to stop the recorder, when there is no sound to detect
- */
- public synchronized void stopRecord(){
- Log.i(TAG, "stopRecord" );
- if (recorder != null && recorderRunning){
- recorder.stop();
- recorder.reset();
- // You can reuse the object by going back to setAudioSource() step
- //recorder.release(); // Now the object cannot be reused
- Toast.makeText(this, "Ending of recording", Toast.LENGTH_SHORT).show();
- activeThread = true;
- Log.i("StopRecord", "Active thread true");
- recorderRunning =false;
- currentAmplitude = INIT_VALUE;
- }
- }
- /***
- * Method run of the thread. It is sending a message to the handler every 2 sec.
- */
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- activeThread = true;
- while(activeThread){
- Log.i(TAG, "onRun()" );
- Thread.sleep(2000);
- threadHandler.sendEmptyMessage(0);
- }
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /***
- * Handler receives the message from the thread, and modify the UIThread as needed
- * Focused on the detection of the amplitude
- */
- private Handler threadHandler = new Handler() {
- public void handleMessage(android.os.Message msg) {
- Log.i(TAG, "handleMessage" );
- int previousValue = currentAmplitude;
- Log.i(TAG, "handleMessage : previous value : "+Integer.toString(currentAmplitude) );
- currentAmplitude = recorder.getMaxAmplitude();
- Log.i(TAG, "handleMessage : MaxAmplitude : "+Integer.toString(currentAmplitude) );
- if (previousValue < AMPLITUDE_END && currentAmplitude < AMPLITUDE_END && recorder != null){
- Log.i(TAG, "handleMessage under 300" );
- stopRecord();
- }
- }
- };
- PS : The activity has to implements the Runnable interface.
Advertisement
Add Comment
Please, Sign In to add comment