2010. 9. 24. 18:43
모바일/안드로이드
출처(source) : http://d.android.com/guide/topics/media/index.html
Audio Capture
Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:
- Create a new instance of
android.media.MediaRecorder
usingnew
- Create a new instance of
android.content.ContentValues
and put in some standard properties likeTITLE
,TIMESTAMP
, and the all importantMIME_TYPE
- 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) - Set the audio source using
MediaRecorder.setAudioSource()
. You will probably want to useMediaRecorder.AudioSource.MIC
- Set output file format using
MediaRecorder.setOutputFormat()
- Set the audio encoder using
MediaRecorder.setAudioEncoder()
- Call
prepare()
on the MediaRecorder instance. - To start audio capture, call
start()
. - To stop audio capture, call
stop()
. - 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 |