What happens If I use following in my wear application?
MediaPlayer.create(this, R.raw.my_audio_file).start();
Will be the file played on Wear device or on its companion handheld, or just nothing will happen? I am asking because I haven't wear device to try it on, only the emulator.
Thanks for the each answer
Wearables with speakers are now supported on API 23. From the docs, first make sure to check if the device has the required API and hardware:
public boolean canPlayAudio(Context context) {
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;
}
If the above returns true, you are set to play sounds on the wearable device just like you would on any other device using the MediaPlayer.
For more details, there is also a sample app available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With