doublesymmetry / KotlinAudio

KotlinAudio is an Android audio player written in Kotlin, making it simpler to work with audio playback from streams and files.
Apache License 2.0
44 stars 70 forks source link

RNTP player does not take focus on play/resume while a different app is playing video #65

Closed mitchdowney closed 1 year ago

mitchdowney commented 1 year ago

We received this bug report recently, where Podverse audio is able to be mixed in with other apps, while video apps play in the background.

If I follow these steps I can reproduce the issue:

1) Play an episode in our app 2) Press pause 3) Open Twitch or YouTube, and play a video 4) Go back into our app while Twitch/YT is playing 5) Resume playing an episode in our app 6) Twitch/YT does not pause

But I cannot reproduce the issue if I follow these steps:

1) Open Twitch or YouTube, and play a video 2) Go back into our app while Twitch/YT is playing. 3) Start playing a new episode in our app. 4) Twitch/YT does pause.

With the help of newdarkworld in the Discord support channel, they pointed me to this documentation.

One thing stands out to me:

Call requestAudioFocus() immediately before starting to play and verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design your app as we describe in this guide, the call to requestAudioFocus() should be made in the onPlay() callback of your media session.

However when I look into KotlinAudio, it appears that requestAudioFocus is called on READY instead of PLAYING. The bug behavior I am seeing seems to fit that? If focus is only called on READY, then our app won't regain audio focus until another track is loaded and the READY state is called?

puckey commented 1 year ago

I am unable to replicate this using the KotlinAudio demo app:

result: spotify pauses

I was unable to replicate your YT instructions, since it pauses playback anyway when you put it into the background (probably need some kind of subscription to have it keep playing)

Which version of RNTP (or KotlinAudio) have you been testing this on? I will test in the RNTP example app also to see if I can replicate there.

(In general it is a good idea when creating an issue like this to make sure you can replicate the issue using on of the example apps, to make sure it isn't a bug in your implementation.)

puckey commented 1 year ago

Also unable to replicate this using the RNTP example app..

mitchdowney commented 1 year ago

@puckey ah ok, sorry about that. I finally know how to make an example app, so I'll give that a try. If I can't reproduce it I'll close this issue.

mitchdowney commented 1 year ago

@puckey it looks like when I disable autoHandleInterruptions in the example app, then I can consistently reproduce the issue. I didn't realize autoHandleInterruptions could pause other apps when our app starts playing. Looks like I just need to set autoHandleInterruptions to true.

Hopefully doing that doesn't break our awkward RemoteDuck handling. At one point a long time ago with v2, there was a setting that combined with remote-duck led to SMS notifications starting/stopping our player, even when it was paused...

Anyway, this appears to be a setting, not a bug. Closing the issue. Thanks for your help!