Advertisement
Guest User

AudioHelper.java

a guest
May 17th, 2010
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package saic.media.audio.clint;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import android.media.MediaRecorder;
  7. import android.os.Environment;
  8.  
  9. public class AudioHelper {
  10.     final MediaRecorder recorder = new MediaRecorder();
  11.       final String path;
  12.  
  13.       /**
  14.        * Creates a new audio recording at the given path (relative to root of SD card).
  15.        */
  16.       public AudioHelper(String path) {
  17.         this.path = sanitizePath(path);
  18.       }
  19.  
  20.       private String sanitizePath(String path) {
  21.         if (!path.startsWith("/")) {
  22.           path = "/" + path;
  23.         }
  24.         if (!path.contains(".")) {
  25.           path += ".3gp";
  26.         }
  27.         return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
  28.       }
  29.  
  30.       /**
  31.        * Starts a new recording.
  32.        */
  33.       public void start() throws IOException {
  34.         String state = android.os.Environment.getExternalStorageState();
  35.         if(!state.equals(android.os.Environment.MEDIA_MOUNTED))  {
  36.             throw new IOException("SD Card is not mounted.  It is " + state + ".");
  37.         }
  38.  
  39.         // make sure the directory we plan to store the recording in exists
  40.         File directory = new File(path).getParentFile();
  41.         if (!directory.exists() && !directory.mkdirs()) {
  42.           throw new IOException("Path to file could not be created.");
  43.         }
  44.  
  45.         recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  46.         recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  47.         recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
  48.         recorder.setOutputFile(path);
  49.         recorder.prepare();
  50.         recorder.start();
  51.       }
  52.  
  53.       /**
  54.        * Stops a recording that has been previously started.
  55.        */
  56.       public void stop() throws IOException {
  57.         recorder.stop();
  58.         //recorder.release();
  59.       }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement