original source : https://developer.android.com/training/wearables/wearable-sounds.html


Using Speakers on Wearables

Detect the Speaker

the app uses the getDevices() method in conjunction with the value of FEATURE_AUDIO_OUTPUT to confirm that the device is equipped with a speaker.

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;

Play Sounds

the process for playing sound on Android Wear is the same as for a handset or other device. For more information, see Media Playback.

If you also want to record audio from the microphone on the wearable, your app must also get permission to use the microphone. To learn more, seePermissions on Android Wear.

Comments are closed.

Post Navigation