블로그 이미지
박황기
최근에 스마트폰에 관심이 많습니다. 예전에 상상하던 모습들이 점차 이루어지고 있는게 신기하네요.

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

Notice

2010. 9. 24. 18:43 모바일/안드로이드

Audio Capture

Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:

  1. Create a new instance of android.media.MediaRecorder using new
  2. Create a new instance of android.content.ContentValues and put in some standard properties like TITLE,TIMESTAMP, and the all important MIME_TYPE
  3. Create a file path for the data to go to (you can use android.content.ContentResolver to create an entry in the Content database and get it to assign a path automatically which you can then use)
  4. Set the audio source using MediaRecorder.setAudioSource(). You will probably want to useMediaRecorder.AudioSource.MIC
  5. Set output file format using MediaRecorder.setOutputFormat()
  6. Set the audio encoder using MediaRecorder.setAudioEncoder()
  7. Call prepare() on the MediaRecorder instance.
  8. To start audio capture, call start().
  9. To stop audio capture, call stop().
  10. When you are done with the MediaRecorder instance, call release() on it.

Example: Audio Capture Setup and Start

The example below illustrates how to set up, then start audio capture.

    recorder = new MediaRecorder();
   
ContentValues values = new ContentValues(3);

    values
.put(MediaStore.MediaColumns.TITLE, SOME_NAME_HERE);
    values
.put(MediaStore.MediaColumns.TIMESTAMP, System.currentTimeMillis());
    values
.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType());
   
   
ContentResolver contentResolver = new ContentResolver();
   
   
Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI;
   
Uri newUri = contentResolver.insert(base, values);
   
   
if (newUri == null) {
       
// need to handle exception here - we were not able to create a new
       
// content entry
   
}
   
   
String path = contentResolver.getDataFilePath(newUri);

   
// could use setPreviewDisplay() to display a preview to suitable View here
   
    recorder
.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder
.setOutputFile(path);
   
    recorder
.prepare();
    recorder
.start();

Stop Recording

Based on the example above, here's how you would stop audio capture.

    recorder.stop();
    recorder
.release();

'모바일 > 안드로이드' 카테고리의 다른 글

Galaxy Tab android  (0) 2010.11.25
Syntax Diagrams For SQLite  (0) 2010.09.30
How to implement Swipe action in Android  (0) 2010.09.23
how to make mp4 for streaming  (0) 2010.07.13
안드로이드 UI  (0) 2010.07.13
posted by 박황기