-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ios): avoid prematurely activating audio session #2146
fix(ios): avoid prematurely activating audio session #2146
Conversation
18983ce
to
dc73578
Compare
This commit fixes the issue where the audio session is prematurely activated when `TrackPlayer.play()` or `TrackPlayer.setPlayWhenReady(true)` is called with an empty queue. - move configureAudioSession from play & setPlayWhenReady to handlePlayWhenReadyChange - add configureAudioSession to handleAudioPlayerCurrentItemChange when we go from no queue items to the first or the other way around - remove setActive parameter from configureAudioSession. Instead, activate the audio session when there is a `currentItem` and `playWhenReady == true`. Deactivate the session when there is no `currentItem`. - in configureAudioSession remove unnecessary ios 13.0 available check, since there is already one for ios 11 - remove `try? audioSessionController.deactivateSession()` before `try? audioSessionController.activateSession()` until we can figure out why it is necessary (see doublesymmetry#2110)
dc73578
to
f92c84f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I've left some small comments so let me know your thoughts, but happy to get this in 🚀
} else if #available(iOS 11.0, *) { | ||
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions) | ||
private func configureAudioSession() { | ||
if (player.currentItem != nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
small nitpick for readability: I'd reverse the order of the conditional to early return "the smaller case"
if (player.currentItem == nil) {
try? audioSessionController.deactivateSession()
return
}
if (player.playWhenReady) {
try? audioSessionController.activateSession()
}
if #available(iOS 11.0, *) {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions)
} else {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, options: sessionCategoryOptions)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added a commit to improve this: 424c326
- deactivate the session when there is no current item to be played
- activate & configure the audio session when there is an item to be played and the player has been configured to start when it is ready loading
@@ -860,6 +856,10 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |||
} | |||
} | |||
|
|||
if ((item != nil && lastItem == nil) || item == nil) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-blocking: There might be some redundancy here, because if you go from no item, to having an item. You'll likely have to have pressed play first to start the playback which would already be covered by handlePlayWhenReadyChange
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
configureAudioSession
now only activates the session when playWhenReady
is true
@@ -908,6 +908,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |||
} | |||
|
|||
func handlePlayWhenReadyChange(playWhenReady: Bool) { | |||
configureAudioSession(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing I just thought of. This will also trigger when going into pause mode, which really shouldn't do anything right (it'll try to activate session again)? Should we be filtering here for just when playWhenReady is true?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are now checking for playWhenReady
in configureAudioSession
- deactivate the session when there is no current item to be played - activate & configure the audio session when there is an item to be played and the player has been configured to start when it is ready loading
657eb21
to
424c326
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good! 👍
This commit fixes the issue where the audio session is prematurely activated when
TrackPlayer.play()
orTrackPlayer.setPlayWhenReady(true)
is called with an empty queue.play
&setPlayWhenReady
tohandlePlayWhenReadyChange
configureAudioSession
tohandleAudioPlayerCurrentItemChange
when we go from no queue items to the first or the other way aroundsetActive
parameter fromconfigureAudioSession
. Instead, activate the audio session when there is acurrentItem
andplayWhenReady == true
. Deactivate the session when there is nocurrentItem
.try? audioSessionController.deactivateSession()
beforetry? audioSessionController.activateSession()
until we can figure out why it is necessary (see [4.0.0-rc06] [iOS] Media player remote center remains invisible while playing audio after running a Unity game on iOS 16 #2110)