Skip to content
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

Merged

Conversation

puckey
Copy link
Collaborator

@puckey puckey commented Oct 2, 2023

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 [4.0.0-rc06] [iOS] Media player remote center remains invisible while playing audio after running a Unity game on iOS 16 #2110)

@puckey puckey requested a review from dcvz as a code owner October 2, 2023 15:52
@puckey puckey force-pushed the fix/premature-audio-session-activation branch from 18983ce to dc73578 Compare October 3, 2023 08:32
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)
@puckey puckey force-pushed the fix/premature-audio-session-activation branch from dc73578 to f92c84f Compare October 3, 2023 08:58
dcvz
dcvz previously approved these changes Oct 10, 2023
Copy link
Contributor

@dcvz dcvz left a 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) {
Copy link
Contributor

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)
}

Copy link
Collaborator Author

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) {
Copy link
Contributor

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.

Copy link
Collaborator Author

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();
Copy link
Contributor

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?

Copy link
Collaborator Author

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
@puckey puckey force-pushed the fix/premature-audio-session-activation branch from 657eb21 to 424c326 Compare October 10, 2023 13:10
Copy link
Contributor

@dcvz dcvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good! 👍

@dcvz dcvz merged commit 1752a7c into doublesymmetry:main Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants