Skip to content

Most visited

Recently visited

navigation

在穿戴式设备上使用扬声器

部分 Android Wear 设备配有扬声器,让开发者可以在其应用中加入声音功能,从而为用户提供多维互动体验。配备扬声器的 Wear 设备可能会触发一个时钟或计时器闹铃,还配有音频通知。如此声色并茂,令 Wear 游戏的娱乐性越来越强。

本页介绍 Wear 上的应用如何使用熟悉的 Android API 通过设备扬声器播放声音。

检测扬声器

Wear 应用必须首先检测穿戴式设备是否有扬声器。在以下示例中,该应用将 getDevices() 函数与 FEATURE_AUDIO_OUTPUT 的值结合使用以确认此设备配有扬声器。

PackageManager packageManager = context.getPackageManager();
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

// Check whether the device has a speaker.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    // Check FEATURE_AUDIO_OUTPUT to guard against false positives.
    if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
        return false;
    }

    AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
    for (AudioDeviceInfo device : devices) {
        if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
            return true;
        }
    }
}
return false;

播放声音

检测到扬声器后,在 Android Wear 上播放声音的流程与在手持式设备或其他设备上播放声音的流程相同。如需了解详细信息,请参阅媒体播放

如果您还想从穿戴式设备上的麦克风录制音频,则您的应用必须获取使用麦克风的权限。如需了解详情,请参阅在 Android Wear 上请求权限。

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)