Guest User

Untitled

a guest
Jan 15th, 2012
2,697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1.     /****
  2.      * Method to start the record of the patient, for the current exercise.
  3.      * Synchornized method because of the thread which is send a message every 2 seconds to detect
  4.      * a sound presence or not.
  5.      * If not : call to stopRecord method
  6.      * @param path
  7.      */
  8.     public synchronized void beginRecord(String path){
  9.  
  10.         Log.i(TAG, "beginRecord" );
  11.         if (!recorderRunning){
  12.             recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  13.             recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
  14.             recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  15.             recorder.setOutputFile(path);
  16.             try {
  17.                 recorder.prepare();
  18.             } catch (IllegalStateException e) {
  19.                 // TODO Auto-generated catch block
  20.                 e.printStackTrace();
  21.             } catch (IOException e) {
  22.                 // TODO Auto-generated catch block
  23.                 e.printStackTrace();
  24.             }
  25.             recorder.start();   // Recording is now started
  26.             recorderRunning = true;
  27.         }
  28.     }
  29.  
  30.     /***
  31.      * Method to stop the recorder, when there is no sound to detect
  32.      */
  33.     public synchronized void stopRecord(){
  34.         Log.i(TAG, "stopRecord" );
  35.         if (recorder != null && recorderRunning){
  36.             recorder.stop();
  37.             recorder.reset();
  38.             // You can reuse the object by going back to setAudioSource() step
  39.             //recorder.release(); // Now the object cannot be reused
  40.             Toast.makeText(this, "Ending of recording", Toast.LENGTH_SHORT).show();
  41.             activeThread = true;
  42.             Log.i("StopRecord", "Active thread true");
  43.             recorderRunning =false;
  44.             currentAmplitude = INIT_VALUE;
  45.         }
  46.  
  47.     }
  48.  
  49.     /***
  50.      * Method run of the thread. It is sending a message to the handler every 2 sec.
  51.      */
  52.     @Override  
  53.     public void run() {
  54.         // TODO Auto-generated method stub         
  55.         try {
  56.             activeThread = true;
  57.             while(activeThread){
  58.                 Log.i(TAG, "onRun()" );
  59.                 Thread.sleep(2000);
  60.                 threadHandler.sendEmptyMessage(0);
  61.  
  62.             }              
  63.         } catch (InterruptedException e) {
  64.             // TODO Auto-generated catch block
  65.             e.printStackTrace();
  66.         }
  67.  
  68.  
  69.     }
  70.  
  71.  
  72.     /***
  73.      * Handler receives the message from the thread, and modify the UIThread as needed
  74.      * Focused on the detection of the amplitude
  75.      */
  76.     private Handler threadHandler = new Handler() {
  77.         public void handleMessage(android.os.Message msg) {
  78.             Log.i(TAG, "handleMessage" );
  79.             int previousValue = currentAmplitude;
  80.             Log.i(TAG, "handleMessage : previous value : "+Integer.toString(currentAmplitude) );
  81.             currentAmplitude = recorder.getMaxAmplitude();
  82.             Log.i(TAG, "handleMessage : MaxAmplitude : "+Integer.toString(currentAmplitude) );
  83.             if (previousValue < AMPLITUDE_END && currentAmplitude < AMPLITUDE_END && recorder != null){
  84.                 Log.i(TAG, "handleMessage under 300" );
  85.                 stopRecord();
  86.             }
  87.  
  88.         }
  89.  
  90.     };
  91.  
  92.  
  93. PS : The activity has to implements the Runnable interface.
Advertisement
Add Comment
Please, Sign In to add comment