Joe Fernandez | 6d78639 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 1 | page.title=Audio Capture |
Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 2 | page.tags=mediarecorder |
Joe Fernandez | 6d78639 | 2011-08-22 15:49:52 -0700 | [diff] [blame] | 3 | @jd:body |
| 4 | |
| 5 | <div id="qv-wrapper"> |
| 6 | <div id="qv"> |
| 7 | |
| 8 | <h2>In this document</h2> |
| 9 | <ol> |
| 10 | <li><a href="#audiocapture">Performing Audio Capture</a> |
| 11 | <ol> |
| 12 | <li><a href='#example'>Code Example</a></li> |
| 13 | </ol> |
| 14 | </li> |
| 15 | </ol> |
| 16 | |
| 17 | <h2>Key classes</h2> |
| 18 | <ol> |
| 19 | <li>{@link android.media.MediaRecorder}</li> |
| 20 | </ol> |
| 21 | |
| 22 | <h2>See also</h2> |
| 23 | <ol> |
| 24 | <li><a href="{@docRoot}guide/appendix/media-formats.html">Android Supported Media Formats</a></li> |
| 25 | <li><a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a></li> |
| 26 | <li><a href="{@docRoot}guide/topics/media/mediaplayer.html">MediaPlayer</a> |
| 27 | </ol> |
| 28 | |
| 29 | </div> |
| 30 | </div> |
| 31 | |
| 32 | <p>The Android multimedia framework includes support for capturing and encoding a variety of common |
| 33 | audio formats, so that you can easily integrate audio into your applications. You can record audio |
| 34 | using the {@link android.media.MediaRecorder} APIs if supported by the device hardware.</p> |
| 35 | |
| 36 | <p>This document shows you how to write an application that captures audio from a device |
| 37 | microphone, save the audio and play it back.</p> |
| 38 | |
| 39 | <p class="note"><strong>Note:</strong> The Android Emulator does not have the ability to capture |
| 40 | audio, but actual devices are likely to provide these capabilities.</p> |
| 41 | |
| 42 | <h2 id="audiocapture">Performing Audio Capture</h2> |
| 43 | |
| 44 | <p>Audio capture from the device is a bit more complicated than audio and video playback, but still |
| 45 | fairly simple:</p> |
| 46 | <ol> |
| 47 | <li>Create a new instance of {@link android.media.MediaRecorder android.media.MediaRecorder}.</li> |
| 48 | <li>Set the audio source using |
| 49 | {@link android.media.MediaRecorder#setAudioSource MediaRecorder.setAudioSource()}. You will |
| 50 | probably want to use |
| 51 | <code>MediaRecorder.AudioSource.MIC</code>.</li> |
| 52 | <li>Set output file format using |
| 53 | {@link android.media.MediaRecorder#setOutputFormat MediaRecorder.setOutputFormat()}. |
| 54 | </li> |
| 55 | <li>Set output file name using |
| 56 | {@link android.media.MediaRecorder#setOutputFile MediaRecorder.setOutputFile()}. |
| 57 | </li> |
| 58 | <li>Set the audio encoder using |
| 59 | {@link android.media.MediaRecorder#setAudioEncoder MediaRecorder.setAudioEncoder()}. |
| 60 | </li> |
| 61 | <li>Call {@link android.media.MediaRecorder#prepare MediaRecorder.prepare()} |
| 62 | on the MediaRecorder instance.</li> |
| 63 | <li>To start audio capture, call |
| 64 | {@link android.media.MediaRecorder#start MediaRecorder.start()}. </li> |
| 65 | <li>To stop audio capture, call {@link android.media.MediaRecorder#stop MediaRecorder.stop()}. |
| 66 | <li>When you are done with the MediaRecorder instance, call |
| 67 | {@link android.media.MediaRecorder#release MediaRecorder.release()} on it. Calling |
| 68 | {@link android.media.MediaRecorder#release MediaRecorder.release()} is always recommended to |
| 69 | free the resource immediately.</li> |
| 70 | </ol> |
| 71 | |
| 72 | <h3 id="example">Example: Record audio and play the recorded audio</h3> |
| 73 | <p>The example class below illustrates how to set up, start and stop audio capture, and to play the |
| 74 | recorded audio file.</p> |
| 75 | <pre> |
| 76 | /* |
| 77 | * The application needs to have the permission to write to external storage |
| 78 | * if the output file is written to the external storage, and also the |
| 79 | * permission to record audio. These permissions must be set in the |
| 80 | * application's AndroidManifest.xml file, with something like: |
| 81 | * |
| 82 | * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> |
| 83 | * <uses-permission android:name="android.permission.RECORD_AUDIO" /> |
| 84 | * |
| 85 | */ |
| 86 | package com.android.audiorecordtest; |
| 87 | |
| 88 | import android.app.Activity; |
| 89 | import android.widget.LinearLayout; |
| 90 | import android.os.Bundle; |
| 91 | import android.os.Environment; |
| 92 | import android.view.ViewGroup; |
| 93 | import android.widget.Button; |
| 94 | import android.view.View; |
| 95 | import android.view.View.OnClickListener; |
| 96 | import android.content.Context; |
| 97 | import android.util.Log; |
| 98 | import android.media.MediaRecorder; |
| 99 | import android.media.MediaPlayer; |
| 100 | |
| 101 | import java.io.IOException; |
| 102 | |
| 103 | |
| 104 | public class AudioRecordTest extends Activity |
| 105 | { |
| 106 | private static final String LOG_TAG = "AudioRecordTest"; |
| 107 | private static String mFileName = null; |
| 108 | |
| 109 | private RecordButton mRecordButton = null; |
| 110 | private MediaRecorder mRecorder = null; |
| 111 | |
| 112 | private PlayButton mPlayButton = null; |
| 113 | private MediaPlayer mPlayer = null; |
| 114 | |
| 115 | private void onRecord(boolean start) { |
| 116 | if (start) { |
| 117 | startRecording(); |
| 118 | } else { |
| 119 | stopRecording(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private void onPlay(boolean start) { |
| 124 | if (start) { |
| 125 | startPlaying(); |
| 126 | } else { |
| 127 | stopPlaying(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | private void startPlaying() { |
| 132 | mPlayer = new MediaPlayer(); |
| 133 | try { |
| 134 | mPlayer.setDataSource(mFileName); |
| 135 | mPlayer.prepare(); |
| 136 | mPlayer.start(); |
| 137 | } catch (IOException e) { |
| 138 | Log.e(LOG_TAG, "prepare() failed"); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private void stopPlaying() { |
| 143 | mPlayer.release(); |
| 144 | mPlayer = null; |
| 145 | } |
| 146 | |
| 147 | private void startRecording() { |
| 148 | mRecorder = new MediaRecorder(); |
| 149 | mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); |
| 150 | mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); |
| 151 | mRecorder.setOutputFile(mFileName); |
| 152 | mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); |
| 153 | |
| 154 | try { |
| 155 | mRecorder.prepare(); |
| 156 | } catch (IOException e) { |
| 157 | Log.e(LOG_TAG, "prepare() failed"); |
| 158 | } |
| 159 | |
| 160 | mRecorder.start(); |
| 161 | } |
| 162 | |
| 163 | private void stopRecording() { |
| 164 | mRecorder.stop(); |
| 165 | mRecorder.release(); |
| 166 | mRecorder = null; |
| 167 | } |
| 168 | |
| 169 | class RecordButton extends Button { |
| 170 | boolean mStartRecording = true; |
| 171 | |
| 172 | OnClickListener clicker = new OnClickListener() { |
| 173 | public void onClick(View v) { |
| 174 | onRecord(mStartRecording); |
| 175 | if (mStartRecording) { |
| 176 | setText("Stop recording"); |
| 177 | } else { |
| 178 | setText("Start recording"); |
| 179 | } |
| 180 | mStartRecording = !mStartRecording; |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | public RecordButton(Context ctx) { |
| 185 | super(ctx); |
| 186 | setText("Start recording"); |
| 187 | setOnClickListener(clicker); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | class PlayButton extends Button { |
| 192 | boolean mStartPlaying = true; |
| 193 | |
| 194 | OnClickListener clicker = new OnClickListener() { |
| 195 | public void onClick(View v) { |
| 196 | onPlay(mStartPlaying); |
| 197 | if (mStartPlaying) { |
| 198 | setText("Stop playing"); |
| 199 | } else { |
| 200 | setText("Start playing"); |
| 201 | } |
| 202 | mStartPlaying = !mStartPlaying; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | public PlayButton(Context ctx) { |
| 207 | super(ctx); |
| 208 | setText("Start playing"); |
| 209 | setOnClickListener(clicker); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | public AudioRecordTest() { |
| 214 | mFileName = Environment.getExternalStorageDirectory().getAbsolutePath(); |
| 215 | mFileName += "/audiorecordtest.3gp"; |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void onCreate(Bundle icicle) { |
| 220 | super.onCreate(icicle); |
| 221 | |
| 222 | LinearLayout ll = new LinearLayout(this); |
| 223 | mRecordButton = new RecordButton(this); |
| 224 | ll.addView(mRecordButton, |
| 225 | new LinearLayout.LayoutParams( |
| 226 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 227 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 228 | 0)); |
| 229 | mPlayButton = new PlayButton(this); |
| 230 | ll.addView(mPlayButton, |
| 231 | new LinearLayout.LayoutParams( |
| 232 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 233 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 234 | 0)); |
| 235 | setContentView(ll); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void onPause() { |
| 240 | super.onPause(); |
| 241 | if (mRecorder != null) { |
| 242 | mRecorder.release(); |
| 243 | mRecorder = null; |
| 244 | } |
| 245 | |
| 246 | if (mPlayer != null) { |
| 247 | mPlayer.release(); |
| 248 | mPlayer = null; |
| 249 | } |
| 250 | } |
| 251 | } |
Joe Fernandez | 33baa5a | 2013-11-14 11:41:19 -0800 | [diff] [blame] | 252 | </pre> |