Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I play audio files on Android Wear?

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

like image 866
romanos Avatar asked Aug 31 '14 12:08

romanos


1 Answers

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.

like image 147
Ricardo Avatar answered Oct 19 '22 22:10

Ricardo