gokadzev / capacitor-music-controls-plugin-new

An update to Cordova Music Controls plugin to support Capacitor 3
9 stars 4 forks source link

Remote (BT headphones e.g) controls not working on Android #5

Open patrickjquinn opened 2 years ago

patrickjquinn commented 2 years ago

Looks like the Bluetooth control events for pause, play, skip song etc are not firing on Android.

For the pause play events i'd suggest trying to request audio focus when the events are fired, and for the skip song (i.e if I double tab play pause on most headphones it triggers the next event on most app), it's likely this was never implemented in the original project.

I've done some experimentation around the above so let me know if you need pointing in the right direction 👍

patrickjquinn commented 2 years ago

I'll give that a go 🙂

Worth pointing out that bluetooth events should probably automatically link up with the remote button events receiver by default.

It stops listening for events on recent Android versions once the app accepting them no longer has audio focus. i.e I implemented

private void askForAudioFocus () {
        mAudioManager.requestAudioFocus(changedListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}

Which I called when an event happened (to ensure we maintain audio focus), on my own fork of the OG repo and that fixed the play/pause issue (also worked when the app was backgrounded).

I think "skip / next" was supposed to be handled by the system on older Android versions (according to a comment i've seen on older forks of the original music controls repo) but that behaviour has since changed.

All in all I think properly hooking bluetooth events is the way to go 👍

Will let you know what I find!