1page.title=Audio and Video 2@jd:body 3 4 <div id="qv-wrapper"> 5 <div id="qv"> 6 7<h2>Audio/Video quickview</h2> 8<ul> 9<li>Audio playback and record</li> 10<li>Video playback</li> 11<li>Handles data from raw resources, files, streams</li> 12<li>Built-in codecs for a variety of media. See <a href="{@docRoot}guide/appendix/media-formats.html">Android Supported Media Formats</a></li> 13</ul> 14 15<h2>Key classes</h2> 16<ol> 17<li>{@link android.media.MediaPlayer MediaPlayer} (all available formats)</li> 18<li>{@link android.media.MediaRecorder MediaRecorder} (all available formats)</li> 19<li>{@link android.media.JetPlayer JetPlayer} (playback, JET content)</li> 20<li>{@link android.media.SoundPool SoundPool} (sound management)</li> 21</ol> 22 23<h2>In this document</h2> 24<ol> 25<li><a href="#playback.html">Audio and Video Playback</a> 26 <ol> 27 <li><a href="#playraw">Playing from a Raw Resource</li> 28 <li><a href="#playfile">Playing from a File or Stream</li> 29 <li><a href="#jet">Playing JET Content</li> 30 </ol> 31</li> 32<li><a href="#capture">Audio Capture</a></li> 33</ol> 34 35<h2>See also</h2> 36<ol> 37<li><a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a></li> 38<li><a href="{@docRoot}guide/topics/media/jet/jetcreator_manual.html">JetCreator User Manual</a></li> 39</ol> 40 41</div> 42</div> 43 44<p>The Android platform offers built-in encoding/decoding for a variety of 45common media types, so that you can easily integrate audio, video, and images into your 46applications. Accessing the platform's media capabilities is fairly straightforward 47— you do so using the same intents and activities mechanism that the rest of 48Android uses.</p> 49 50<p>Android lets you play audio and video from several types of data sources. You 51can play audio or video from media files stored in the application's resources 52(raw resources), from standalone files in the filesystem, or from a data stream 53arriving over a network connection. To play audio or video from your 54application, use the {@link android.media.MediaPlayer} class.</p> 55 56<p>The platform also lets you record audio and video, where supported by the 57mobile device hardware. To record audio or video, use the {@link 58android.media.MediaRecorder} class. Note that the emulator doesn't have hardware 59to capture audio or video, but actual mobile devices are likely to provide these 60capabilities, accessible through the MediaRecorder class. </p> 61 62<p>For a list of media formats for which Android offers built-in support, 63see the <a href="{@docRoot}guide/appendix/media-formats.html">Android Media 64Formats</a> appendix. </p> 65 66<h2 id="play">Audio and Video Playback</h2> 67<p>Media can be played from anywhere: from a raw resource, from a file from the system, 68or from an available network (URL).</p> 69 70<p>You can play back the audio data only to the standard 71output device; currently, that is the mobile device speaker or Bluetooth headset. You 72cannot play sound files in the conversation audio. </p> 73 74<h3 id="playraw">Playing from a Raw Resource</h3> 75<p>Perhaps the most common thing to want to do is play back media (notably sound) 76within your own applications. Doing this is easy:</p> 77<ol> 78 <li>Put the sound (or other media resource) file into the <code>res/raw</code> 79 folder of your project, where the Eclipse plugin (or aapt) will find it and 80 make it into a resource that can be referenced from your R class</li> 81 <li>Create an instance of <code>MediaPlayer</code>, referencing that resource using 82 {@link android.media.MediaPlayer#create MediaPlayer.create}, and then call 83 {@link android.media.MediaPlayer#start() start()} on the instance:</li> 84</ol> 85<pre> 86 MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); 87 mp.start(); 88</pre> 89<p>To stop playback, call {@link android.media.MediaPlayer#stop() stop()}. If 90you wish to later replay the media, then you must 91{@link android.media.MediaPlayer#reset() reset()} and 92{@link android.media.MediaPlayer#prepare() prepare()} the MediaPlayer object 93before calling {@link android.media.MediaPlayer#start() start()} again. 94(<code>create()</code> calls <code>prepare()</code> the first time.)</p> 95<p>To pause playback, call {@link android.media.MediaPlayer#pause() pause()}. 96Resume playback from where you paused with 97{@link android.media.MediaPlayer#start() start()}.</p> 98 99<h3 id="playfile">Playing from a File or Stream</h3> 100<p>You can play back media files from the filesystem or a web URL:</p> 101<ol> 102 <li>Create an instance of the <code>MediaPlayer</code> using <code>new</code></li> 103 <li>Call {@link android.media.MediaPlayer#setDataSource setDataSource()} 104 with a String containing the path (local filesystem or URL) 105 to the file you want to play</li> 106 <li>First {@link android.media.MediaPlayer#prepare prepare()} then 107 {@link android.media.MediaPlayer#start() start()} on the instance:</li> 108</ol> 109<pre> 110 MediaPlayer mp = new MediaPlayer(); 111 mp.setDataSource(PATH_TO_FILE); 112 mp.prepare(); 113 mp.start(); 114</pre> 115<p>{@link android.media.MediaPlayer#stop() stop()} and 116{@link android.media.MediaPlayer#pause() pause()} work the same as discussed 117above.</p> 118 <p class="note"><strong>Note:</strong> It is possible that <code>mp</code> could be 119 null, so good code should <code>null</code> check after the <code>new</code>. 120 Also, <code>IllegalArgumentException</code> and <code>IOException</code> either 121 need to be caught or passed on when using <code>setDataSource()</code>, since 122 the file you are referencing may not exist.</p> 123<p class="note"><strong>Note:</strong> 124If you're passing a URL to an online media file, the file must be capable of 125progressive download.</p> 126 127<h3 id="jet">Playing JET content</h3> 128<p>The Android platform includes a JET engine that lets you add interactive playback of JET audio content in your applications. You can create JET content for interactive playback using the JetCreator authoring application that ships with the SDK. To play and manage JET content from your application, use the {@link android.media.JetPlayer JetPlayer} class.</p> 129 130<p>For a description of JET concepts and instructions on how to use the JetCreator authoring tool, see the <a href="{@docRoot}guide/topics/media/jet/jetcreator_manual.html">JetCreator User Manual</a>. The tool is available fully-featured on the OS X and Windows platforms and the Linux version supports all the content creation features, but not the auditioning of the imported assets. </p> 131 132<p>Here's an example of how to set up JET playback from a .jet file stored on the SD card:</p> 133 134<pre> 135JetPlayer myJet = JetPlayer.getJetPlayer(); 136myJet.loadJetFile("/sdcard/level1.jet"); 137byte segmentId = 0; 138 139// queue segment 5, repeat once, use General MIDI, transpose by -1 octave 140myJet.queueJetSegment(5, -1, 1, -1, 0, segmentId++); 141// queue segment 2 142myJet.queueJetSegment(2, -1, 0, 0, 0, segmentId++); 143 144myJet.play(); 145</pre> 146 147<p>The SDK includes an example application — JetBoy — that shows how to use {@link android.media.JetPlayer JetPlayer} to create an interactive music soundtrack in your game. It also illustrates how to use JET events to synchronize music and game logic. The application is located at <code><sdk>/platforms/android-1.5/samples/JetBoy</code>. 148 149<h2 id="capture">Audio Capture</h2> 150<p>Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:</p> 151<ol> 152 <li>Create a new instance of {@link android.media.MediaRecorder 153 android.media.MediaRecorder} using <code>new</code></li> 154 <li>Create a new instance of {@link android.content.ContentValues 155 android.content.ContentValues} and put in some standard properties like 156 <code>TITLE</code>, <code>TIMESTAMP</code>, and the all important 157 <code>MIME_TYPE</code></li> 158 <li>Create a file path for the data to go to (you can use {@link 159 android.content.ContentResolver android.content.ContentResolver} to 160 create an entry in the Content database and get it to assign a path 161 automatically which you can then use)</li> 162 <li>Set the audio source using {@link android.media.MediaRecorder#setAudioSource 163 MediaRecorder.setAudioSource()}. You will probably want to use 164 <code>MediaRecorder.AudioSource.MIC</code></li> 165 <li>Set output file format using {@link 166 android.media.MediaRecorder#setOutputFormat MediaRecorder.setOutputFormat()} 167 </li> 168 <li>Set the audio encoder using 169 {@link android.media.MediaRecorder#setAudioEncoder MediaRecorder.setAudioEncoder()} 170 </li> 171 <li>Call {@link android.media.MediaRecorder#prepare prepare()} 172 on the MediaRecorder instance.</li> 173 <li>To start audio capture, call 174 {@link android.media.MediaRecorder#start start()}. </li> 175 <li>To stop audio capture, call {@link android.media.MediaRecorder#stop stop()}. 176 <li>When you are done with the MediaRecorder instance, call 177{@link android.media.MediaRecorder#release release()} on it. </li> 178</ol> 179 180<h3>Example: Audio Capture Setup and Start</h3> 181<p>The example below illustrates how to set up, then start audio capture.</p> 182<pre> 183 recorder = new MediaRecorder(); 184 ContentValues values = new ContentValues(3); 185 186 values.put(MediaStore.MediaColumns.TITLE, SOME_NAME_HERE); 187 values.put(MediaStore.MediaColumns.TIMESTAMP, System.currentTimeMillis()); 188 values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType()); 189 190 ContentResolver contentResolver = new ContentResolver(); 191 192 Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI; 193 Uri newUri = contentResolver.insert(base, values); 194 195 if (newUri == null) { 196 // need to handle exception here - we were not able to create a new 197 // content entry 198 } 199 200 String path = contentResolver.getDataFilePath(newUri); 201 202 // could use setPreviewDisplay() to display a preview to suitable View here 203 204 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); 205 recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 206 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 207 recorder.setOutputFile(path); 208 209 recorder.prepare(); 210 recorder.start(); 211</pre> 212<h3>Stop Recording</h3> 213<p>Based on the example above, here's how you would stop audio capture. </p> 214<pre> 215 recorder.stop(); 216 recorder.release(); 217</pre> 218 219