jellyfin / jellyfin-android

Android Client for Jellyfin
https://jellyfin.org
GNU General Public License v2.0
1.48k stars 244 forks source link

Pause playback on Bluetooth disconnect #1116

Closed minty-boo closed 1 year ago

minty-boo commented 1 year ago

Describe the feature you'd like Automatically pause music/video playback on Bluetooth headphones/earbuds/speaker disconnect (optionally allow behaviour to be toggled).

Additional context Most other streaming services' apps implement this behaviour, which is preferable for music listening; e.g. if the device is in a back pocket/bag, and TWS earphones are placed into their case (thus disconnecting), it currently continues to play the content over the device's built-in speaker(s).

Though, edge cases (e.g. switching to showing another person what you're watching/listening to, from it playing solely on the Bluetooth device) may exist, hence the toggleability suggestion.

SegiH commented 1 year ago

I looked at the code and I think that I know what can be changed to allow for this behavior that you are asking for where the playback status will toggle (pause if its playing, play if its paused) when Bluetooth is disconnected.

I have not tested this yet but I hope one of the devs can confirm that this should work.

In app/src/main/java/org/jellyfin/mobile/webapp/RemotePlayerService.kt#110, there is a event handler when the Bluetooth is disconnected.

After webappFunctionChannel.callPlaybackManagerAction(PLAYBACK_MANAGER_COMMAND_PAUSE) on line 110

I added this:

                    val currentState = playbackState ?: return

                    val canSeek = (currentState.actions and PlaybackState.ACTION_SEEK_TO) != 0L

                    val isPlaying = currentState.state == PlaybackState.STATE_PLAYING

                    var position = currentState.position

                    setPlaybackState(!isPlaying,position,canSeek)
rahaaatul commented 1 year ago

@SegiH can you also add, bluetooth ear bud controls? like tap to play and pause? Nothing happens when I do that. It should play or pause when I tap my ear buds.

SegiH commented 1 year ago

I'm not sure if I will be able to add that specific feature.

rahaaatul commented 1 year ago

Why not? I mean every video player have that feature function...

guerda commented 1 year ago

I looked at the code and I think that I know what can be changed to allow for this behavior that you are asking for where the playback status will toggle (pause if its playing, play if its paused) when Bluetooth is disconnected.

I have not tested this yet but I hope one of the devs can confirm that this should work.

In app/src/main/java/org/jellyfin/mobile/webapp/RemotePlayerService.kt#110, there is a event handler when the Bluetooth is disconnected.

After webappFunctionChannel.callPlaybackManagerAction(PLAYBACK_MANAGER_COMMAND_PAUSE) on line 110

Would that mean that if I connect a random device while Jellyfin not playing, the playback would start? I don't think that this is helpful. I think the natural default would be to stop playback once a Bluetooth device disconnects.

Right now, this is not the case, even though it's stated in the code: https://github.com/jellyfin/jellyfin-android/blob/master/app/src/main/java/org/jellyfin/mobile/webapp/RemotePlayerService.kt#L110

minty-boo commented 1 year ago

.. I think the natural default would be to stop playback once a Bluetooth device disconnects.

This is what I intended to suggest. Perhaps I should have clarified after @SegiH their suggestion.

In my opinion this should be the default behaviour. Though I cannot speak for others' preferences, or possible edge cases. Hence, the suggestion to make the behaviour customizable (likely just a toggle to enable/disable the behaviour, not to toggle the playback state).

Right now, this is not the case, even though it's stated in the code

Perhaps this is due to the code checking for STATE_AUDIO_CONNECTED, which docs state is used for SCO (low BW, e.g. handset profile), rather than A2DP. Maybe checking the (dis)connected state and profile (handset or A2DP in either event), would fix this issue and result in the intended behaviour?

minty-boo commented 1 year ago

Excuse me sausage-fingering the close button.