Playing audio on wearables

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. In the following example, the app uses the getDevices() method in conjunction with the value of FEATURE_AUDIO_OUTPUT to enumerate all audio outputs.

Developers can expect wearables to have one or both of the following audio outputs available:

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 may be paired or unpaired while your app is running. An app may 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've detected a suitable audio output, the process for playing audio on Wear OS is the same as a mobile or other device. For more information, see MediaPlayer overview. For easier access to more advanced features, such as streaming and downloading media, consider using ExoPlayer.

Use speakers

Some Wear OS devices include speakers, enabling them to incorporate sound into their apps and offer an extra dimension of engagement with the user. A speaker-equipped Wear OS device might trigger a clock or timer alarm, complete with audio notification. Fitness apps may also use the speaker to provide exercise instructions.

Refer to the following related resources: