Advertisement
AntonioLinux

ex2

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