1page.title=Using Speakers on Wearables 2 3@jd:body 4 5<div id="tb-wrapper"> 6<div id="tb"> 7<h2>This lesson teaches you to</h2> 8<ol> 9 <li><a href="#Detect">Detect the Speaker</a></li> 10 <li><a href="#Play">Play Sounds</a></li> 11</ol> 12<h2>You should also read</h2> 13<ul> 14 <li><a href="{@docRoot}design/wear/watchfaces.html">Watch Faces for Android Wear</a></li> 15</ul> 16</div> 17</div> 18 19<p>Some Android Wear devices include speakers, enabling them to incorporate sound into their 20apps and offer an extra dimension of engagement with the user. A speaker-equipped Wear device might 21trigger a clock or timer alarm, complete with audio notification. Games on Wear become become more 22entertaining by offering not just sight, but sound.</p> 23 24<p>This page describes how apps on Wear devices running Android 6.0 (API level 23) can use 25familiar Android APIs to play sounds through the device speaker.</p> 26 27<h2 id="Detect">Detect the Speaker</h2> 28 29<p>A Wear app must first detect whether the wearable device has a speaker. In the following example, 30the app uses the {@link android.media.AudioManager#getDevices(int) getDevices() } method in 31conjunction with the value of {@link android.content.pm.PackageManager#FEATURE_AUDIO_OUTPUT} to 32confirm that the device is equipped with a speaker.</p> 33 34<pre> 35PackageManager packageManager = context.getPackageManager(); 36AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 37 38// Check whether the device has a speaker. 39if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 40 // Check FEATURE_AUDIO_OUTPUT to guard against false positives. 41 if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) { 42 return false; 43 } 44 45 AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS); 46 for (AudioDeviceInfo device : devices) { 47 if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { 48 return true; 49 } 50 } 51} 52return false; 53</pre> 54 55<h2 id="Play">Play Sounds</h2> 56 57<p>Once you've detected the speaker, the process for playing sound on Android Wear is the 58same as for a handset or other device. For more information, see 59<a href="{@docRoot}guide/topics/media/mediaplayer.html">Media Playback</a>.</p> 60 61<p>If you also want to record audio from the microphone on the wearable, your app must also get 62permission to use the microphone. To learn more, see 63<a href="{@docRoot}training/articles/wear-permissions.html">Permissions on Android Wear.</a></p>