Can I play audio files on Android Wear?

What will happen if I use the following in my wear app?

MediaPlayer.create(this, R.raw.my_audio_file).start(); 

Will the file play on the Wear device or its companion, or just nothing happens? I ask because I do not wear the device to try it, only an emulator.

Thanks for every answer.

+5
source share
2 answers

Worn-out devices do not have speakers, so you wonโ€™t hear anything, this can lead to an error.

A better approach would be to use messaging to send messages from the app to be worn in the mobile app and get it to play on the mobile device.

See this post for an example of messaging from wear to mobile.

+3
source

Media with speakers are now supported by API 23. From docs, first make sure that 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 value returns true, you are set to play sounds on the wearable device, like on any other device, using MediaPlayer .

There is also an example application for more information.

+4
source

Source: https://habr.com/ru/post/1201501/


All Articles