AudioManageraudioManager=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);funaudioOutputAvailable(type:Int):Boolean{if(!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)){returnfalse}returnaudioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS).any{it.type==type}}// True if the device has a speakeraudioOutputAvailable(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)// True if a Bluetooth headset is paired and connectedaudioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)// True if a BLE broadcast group device is paired and connectedaudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST)// True if a BLE headset is paired and connectedaudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET)// True if a BLE speaker is paired and connectedaudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER)
为了提供最佳用户体验,您的应用应确保仅在有蓝牙耳机或音箱连接到手表时播放媒体。
选择用于输出音频的首选设备
根据应用的使用情形以及音频对应用核心体验的重要性,选择您希望用户与应用音频输出之间的互动方式。
允许用户选择媒体输出设备
从 Wear OS 5 开始,系统提供了一个界面,让用户可以选择哪个设备应播放媒体,并显示有关当前播放的媒体内容的信息。
如果您的应用在您想在搭载 Wear OS 5 或更高版本的设备上提供音频播放时检测到未连接蓝牙耳机,请提出将用户直接转到媒体输出切换器。在不支持媒体输出切换器的设备上,调用 ACTION_BLUETOOTH_SETTINGS intent 操作,该操作会将用户带到系统设置中的蓝牙页面。
audioManager.registerAudioDeviceCallback(object:AudioDeviceCallback(){overridefunonAudioDevicesAdded(addedDevices:Array<outAudioDeviceInfo>?){super.onAudioDevicesAdded(addedDevices)if(audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)||audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST)||audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET)||audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER)){// A Bluetooth or BLE device is connected and available for playback.}}overridefunonAudioDevicesRemoved(removedDevices:Array<outAudioDeviceInfo>?){super.onAudioDevicesRemoved(removedDevices)if(!(audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)) && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST)) && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET)) && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER))){// No Bluetooth or BLE devices are connected anymore.}}},null)
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Play audio on wearables\n\nThis guide describes how apps on Wear OS can use familiar Android APIs to play audio.\n\nDetect audio devices\n--------------------\n\n\nA Wear OS app must first detect whether the wearable device has an appropriate audio output.\nDevelopers can expect wearables to have at least one of the following audio outputs available:\n\n- [AudioDeviceInfo.TYPE_BUILTIN_SPEAKER](/reference/android/media/AudioDeviceInfo#TYPE_BUILTIN_SPEAKER): on devices that have a built-in speaker\n- [AudioDeviceInfo.TYPE_BLUETOOTH_A2DP](/reference/android/media/AudioDeviceInfo#TYPE_BLUETOOTH_A2DP): when a Bluetooth headset is paired and connected\n- [AudioDeviceInfo.TYPE_BLE_BROADCAST](/reference/android/media/AudioDeviceInfo#TYPE_BLE_BROADCAST): when a Bluetooth Low Energy (BLE) broadcast group device is paired and connected\n- [AudioDeviceInfo.TYPE_BLE_HEADSET](/reference/android/media/AudioDeviceInfo#TYPE_BLE_HEADSET): when a BLE headset is paired and connected\n- [AudioDeviceInfo.TYPE_BLE_SPEAKER](/reference/android/media/AudioDeviceInfo#TYPE_BLE_SPEAKER): when a BLE speaker is paired and connected\n\n\nIn the following example, the app uses the\n[getDevices()](/reference/android/media/AudioManager#getDevices(int))\nmethod in conjunction with the value of `FEATURE_AUDIO_OUTPUT` to enumerate all audio\noutputs. \n\n```kotlin\nAudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);\n\nfun audioOutputAvailable(type: Int): Boolean {\n if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {\n return false\n }\n return audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS).any { it.type == type }\n}\n\n// True if the device has a speaker\naudioOutputAvailable(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER)\n\n// True if a Bluetooth headset is paired and connected\naudioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)\n\n// True if a BLE broadcast group device is paired and connected\naudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST)\n\n// True if a BLE headset is paired and connected\naudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET)\n\n// True if a BLE speaker is paired and connected\naudioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER)\n```\n\nTo give the best user experience, your app should make sure to only play media when Bluetooth\nheadphones or speakers are connected to the watch.\n\nChoose preferred device for audio output\n----------------------------------------\n\nDepending on your app's use case, and the importance of audio to your app's core experience,\nchoose how you'd like users to engage with your app's audio output.\n\n### Let user choose media output device\n\nStarting in Wear OS 5, the system provides a UI that lets users choose which\ndevice should play media and show information about the currently-playing media\ncontent.\n\nIf your app detects that there isn't a Bluetooth headset connected when you want\nto provide audio playback on devices running Wear OS 5 or higher, offer to take\nthe user directly to media output switcher. On devices that don't support the\nmedia output switcher, invoke the [`ACTION_BLUETOOTH_SETTINGS`](/reference/android/provider/Settings#ACTION_BLUETOOTH_SETTINGS) intent\naction, which takes the user to the Bluetooth page in system settings.\n\nThe [`launchOutputSelection()`](https://github.com/google/horologist/blob/v0.5.26/media/audio/src/main/java/com/google/android/horologist/audio/SystemAudioRepository.kt#L108) method, part of the [Horologist](https://github.com/google/horologist) library\non GitHub, demonstrates how to let the user choose their media output device.\n\n### Bluetooth headset\n\n\nUnlike built-in speakers, which are always available if present on the device, a Bluetooth headset\ncan be paired or unpaired while an app runs. If the app requires a headset to continue,\nregister a callback to detect when\nthe user connects and disconnects a Bluetooth headset using\n[registerAudioDeviceCallback](/reference/android/media/AudioManager#registerAudioDeviceCallback(android.media.AudioDeviceCallback,%20android.os.Handler)):\n\n\u003cbr /\u003e\n\n```kotlin\naudioManager.registerAudioDeviceCallback(object : AudioDeviceCallback() {\n override fun onAudioDevicesAdded(addedDevices: Array\u003cout AudioDeviceInfo\u003e?) {\n super.onAudioDevicesAdded(addedDevices)\n if (audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP)\n || audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST)\n || audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET)\n || audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER)) {\n // A Bluetooth or BLE device is connected and available for playback.\n }\n }\n override fun onAudioDevicesRemoved(removedDevices: Array\u003cout AudioDeviceInfo\u003e?) {\n super.onAudioDevicesRemoved(removedDevices)\n if (!(audioOutputAvailable(AudioDeviceInfo.TYPE_BLUETOOTH_A2DP))\n && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_BROADCAST))\n && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_HEADSET))\n && !(audioOutputAvailable(AudioDeviceInfo.TYPE_BLE_SPEAKER))) {\n // No Bluetooth or BLE devices are connected anymore.\n }\n }\n}, null)\n \n```\n\n\u003cbr /\u003e\n\nIf your app detects that there isn't a Bluetooth headset connected when you want to provide audio\noutput, don't show an error message. Instead, offer to take the user directly to Bluetooth settings\nto make it easier for them to connect. This can be done by sending an intent with\n[ACTION_BLUETOOTH_SETTINGS](/reference/android/provider/Settings#ACTION_BLUETOOTH_SETTINGS):\n\n\u003cbr /\u003e\n\n```kotlin\n val intent = with (Intent(Settings.ACTION_BLUETOOTH_SETTINGS)) {\n addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)\n putExtra(\"EXTRA_CONNECTION_ONLY\", true)\n putExtra(\"EXTRA_CLOSE_ON_CONNECT\", true)\n putExtra(\"android.bluetooth.devicepicker.extra.FILTER_TYPE\", 1)\n }\n startActivity(intent)\n \n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Built-in speakers\n\n\nMost Wear OS devices have built-in speakers. If your app offers a non-media use case that incorporates\nsound, consider using speakers to offer an extra dimension of engagement with the user. For\nexample, a speaker-equipped Wear OS device might trigger a clock or timer alarm, complete with\naudio notification, and fitness apps might use the speaker to provide exercise instructions.\n\n**Note:**Built-in speakers don't deliver the best experience for listening to media\ncontent as they aren't designed for this purpose.\n\n\nRefer to the [WearSpeakerSample](https://github.com/android/wear-os-samples/tree/main/WearSpeakerSample) for details.\n\n\u003cbr /\u003e\n\nPlay audio\n----------\n\n\nOnce you detect and choose a suitable audio output, the process for playing audio on Wear OS is the same\nas on mobile or other devices. For more information, see [MediaPlayer overview](/guide/topics/media/mediaplayer).\nFor easier access to more advanced features, such as streaming and downloading media, use\n[ExoPlayer](/guide/topics/media/exoplayer).\nMake sure to follow best practices for Audio apps such as [Managing Audio focus.](/guide/topics/media/media-apps/audio-focus)\n\n### Prevent unintended media playback through built-in speakers\n\n| **Note:** If you are targeting Android 15 (API level 35) or higher, the media output switcher provides the watch built-in speakers as one of the selectable options on supported Wear OS devices. To support this feature when using Exoplayer, update to version 1.5.0 or later version.\n\n\u003cbr /\u003e\n\n\nMedia apps can follow the following guidance to make sure the app does not unintentionally play\nmedia content on built-in watch speakers. The guidance is different based on which player the\napp is using.\n\n#### ExoPlayer\n\n\nIf your app is using ExoPlayer:\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n1. Call [setSuppressPlaybackOnUnsuitableOutput(true)](/reference/kotlin/androidx/media3/exoplayer/ExoPlayer.Builder#setSuppressPlaybackOnUnsuitableOutput(boolean))\n while building the ExoPlayer instance:\n\n ```kotlin\n ExoPlayer exoplayer = ExoPlayer.Builder(context)\n .setAudioAttributes(...)\n .setSuppressPlaybackWhenUnsuitableOutput(true)\n // ...\n .build()\n \n ```\n2. React to the playback suppression event by registering\n [WearUnsuitableOutputPlaybackSuppressionResolverListener](/reference/kotlin/androidx/media3/ui/WearUnsuitableOutputPlaybackSuppressionResolverListener)\n as a listener of the ExoPlayer instance:\n\n```kotlin\n exoPlayer.addListener(WearUnsuitableOutputPlaybackSuppressionResolverListener(context))\n \n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n#### Horologist Media toolkit\n\n\nThe [Horologist MediaToolkit](https://google.github.io/horologist/media-toolkit/) already contains logic to prevent unintended media playback on built-in watch speakers.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n#### Other media players\n\n- Make sure that media audio playback doesn't start unless a suitable output device, such as a headset or a set of external speakers, is [connected](/reference/android/media/AudioManager#getDevices(int)) to the watch. See the following list for suitable output devices for media apps:\n - [`TYPE_BLUETOOTH_A2DP`](/reference/android/media/AudioDeviceInfo#TYPE_BLUETOOTH_A2DP)\n - [`TYPE_BLE_BROADCAST`](/reference/android/media/AudioDeviceInfo#TYPE_BLE_BROADCAST)\n - [`TYPE_BLE_HEADSET`](/reference/android/media/AudioDeviceInfo#TYPE_BLE_HEADSET)\n - [`TYPE_BLE_SPEAKER`](/reference/android/media/AudioDeviceInfo#TYPE_BLE_SPEAKER)\n- Pause the playback if [AudioManager](/reference/android/media/AudioManager) notifies your app that an external audio output device [becomes disconnected from the watch](/reference/android/media/AudioDeviceCallback#onAudioDevicesRemoved(android.media.AudioDeviceInfo[])).\n- When the user attempts to initiate media playback but hasn't connected an external audio device, [prompt the user to connect](/training/wearables/apps/audio#prompt-the-user-to-connect-a-headset) such a device to their watch.\n\n\u003cbr /\u003e"]]