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.setAudioSourcef). You will probably want to use MediaRecorder.AudioSource.MIC

5. Set output file format using MediaRecorder.setOutputFormatQ

6. Set the audio encoder using MediaRecorder.setAudioEncoderQ

7. Call prepareQ on the MediaRecorder instance.

8. To start audio capture, call startQ.

9. To stop audio capture, call stopf).

10. When you are done with the MediaRecorder instance, call releasef) 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, S0ME_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_C0NTENT_URI;

Uri newUri = contentResolver.insert(base, values);

//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.set0utputFormat(MediaRecorder.0utputFormat.THREE GPP);

recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR NB);

recorder.set0utputFile(path);

recorder.prepare();

recorder.start();

Stop Recording

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

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

0 0

Post a comment