ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • AudioRecord ~~
    과거...../개발일지 2010. 5. 18. 21:59

     많은 사람들이 밑에 소스가 안된다고 한다.
     안되는 이유를 검색하던 도중 알았다.
     퍼미션이 안되있었다는거.....참..ㅜ.ㅜ(한글 사이트에는 안나와있음)
     다른분들도 주의 바랍니다.

    추가해야되는 퍼미션 2가지
     <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    package com.record;
    import java.io.File;
    import java.io.IOException;

    import android.media.MediaRecorder;
    import android.os.Environment;
    import android.util.Log;


    public class AudioRecord {
     
     final MediaRecorder recorder = new MediaRecorder();
     final String path;
     
     
     public AudioRecord(String path){
      this.path = sanitizePath(path); 
      
     }
     
     private String sanitizePath(String path){
      if(!path.startsWith("/")){
       path = "/" + path;
      }  
      if(!path.contains(".")){
       path += ".3gp";
      }
      return Environment.getExternalStorageDirectory().getAbsolutePath() + path;  
     }
     
     
     public void start() throws IOException{
      String state = android.os.Environment.getExternalStorageState();
      
      if(!state.equals(android.os.Environment.MEDIA_MOUNTED)){
       throw new IOException("SD Card is not mounted. It is " + state  +".");
      }
      
      File directory = new File(path).getParentFile();
      
      if(!directory.exists() && ! directory.mkdir()){
       throw new IOException("path to file couldn't be created");
      }  
      
      recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
           
            recorder.setOutputFile(path);
            recorder.prepare();
            recorder.start();
     }
     
     
     public void stop() throws IOException{
      recorder.stop();
      recorder.release();
     }
     

    }

Designed by Tistory.