This guide describes how apps on Wear OS can use familiar Android APIs to play audio.
Detect audio devices
A Wear OS app must first detect whether the wearable device has an appropriate audio output. Developers can expect wearables to have one or both of the following audio outputs available:
AudioDeviceInfo.TYPE_BUILTIN_SPEAKER
: on devices that have a built-in speakerAudioDeviceInfo.TYPE_BLUETOOTH_A2DP
: when a Bluetooth headset is paired and connected
In the following example, the app uses the
getDevices()
method in conjunction with the value of FEATURE_AUDIO_OUTPUT
to enumerate all audio
outputs.
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); fun audioOutputAvailable(type: Int): Boolean { if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) { return false } return audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS).any { it.type == type } } audioOutputAvailable(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) // True if the device has a speaker audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP) // True if a Bluetooth headset is connected
Wait for a Bluetooth headset
Unlike built-in speakers, which are always available if present on the device, a Bluetooth headset
can be paired or unpaired while an app runs. Your app can register a callback to detect when
this happens using
registerAudioDeviceCallback
:
audioManager.registerAudioDeviceCallback(object : AudioDeviceCallback() { override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) { super.onAudioDevicesAdded(addedDevices) if (audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)) { // A Bluetooth headset has just been connected } } override fun onAudioDevicesRemoved(removedDevices: Array<out AudioDeviceInfo>?) { super.onAudioDevicesRemoved(removedDevices) if (!audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)) { // A Bluetooth headset is no longer connected } } }, null)
Prompt the user to connect a headset
If the app requires a headset to continue, instead of showing an error message, offer to take the
user directly to Bluetooth settings to make it easier for them to connect. This can be done
by sending an intent with ACTION_BLUETOOTH_SETTINGS
:
val intent = with (Intent(Settings.ACTION_BLUETOOTH_SETTINGS)) { addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK) putExtra("EXTRA_CONNECTION_ONLY", true) putExtra("EXTRA_CLOSE_ON_CONNECT", true) putExtra("android.bluetooth.devicepicker.extra.FILTER_TYPE", 1) } startActivity(intent)
Play audio
Once you detect a suitable audio output, the process for playing audio on Wear OS is the same as on mobile or other devices. For more information, see MediaPlayer overview. For easier access to more advanced features, such as streaming and downloading media, use ExoPlayer. Make sure to follow best practices for Audio apps such as Managing Audio focus
.Use speakers
Some Wear OS devices include speakers. Enable speakers to incorporate sound into apps for those devices and to offer an extra dimension of engagement with the user. For example, a speaker-equipped Wear OS device might trigger a clock or timer alarm, complete with audio notification, and fitness apps might use the speaker to provide exercise instructions.
Refer to the WearSpeakerSample for details.