romanos
romanos

Reputation: 1338

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

Upvotes: 5

Views: 1323

Answers (3)

Will Shaw
Will Shaw

Reputation: 52

Wrong, Samsung Galaxy watch 4 and 5 have speaker, and have wear os, also you can play audio from watch itself and connect bluetooth, earphones, to listen to music, and control your music on phone too, go to android developer in kotlin, will tell you the outputs to play songs, I heard some google wear os devices they have, a built in speaker. Maybe not googles own though. Yes you can play audio files on wear os, check on developer android website, btw for me I did not need to add extra code for audio output, to work as I'm using a service, and or because coding like a normal app, probably cause of using service, however putting audio output code, on service and or activity made it sound better, possibly in own media player class to, if you want to play sound from watch, choose the audioOutputAvailable(AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) // True if the device has a speaker to play on watch itself remember, to add full code of audio output code choosing built in only feature if you want.

Upvotes: 0

Ricardo
Ricardo

Reputation: 7965

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.

Upvotes: 5

CodeChimp
CodeChimp

Reputation: 4835

Wear devices have no speaker so you will not hear anything, it may error.

Best approach would be to use the messaging to send a message from wear app to the mobile app and get that to play it on the mobile.

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

Upvotes: 3

Related Questions