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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions ios/RNTrackPlayer/RNTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,18 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
}


private func configureAudioSession(setActive: Bool = false) {
if #available(iOS 13.0, *) {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, policy: sessionCategoryPolicy, options: sessionCategoryOptions)
} 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

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)
}
} else {
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, options: sessionCategoryOptions)
}
if (setActive) {
try? audioSessionController.deactivateSession()
try? audioSessionController.activateSession()
}
}

Expand Down Expand Up @@ -521,8 +522,6 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
@objc(play:rejecter:)
public func play(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
if (rejectWhenNotInitialized(reject: reject)) { return }

configureAudioSession(setActive: true)
player.play()
resolve(NSNull())
}
Expand All @@ -538,9 +537,6 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
@objc(setPlayWhenReady:resolver:rejecter:)
public func setPlayWhenReady(playWhenReady: Bool, resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) {
if (rejectWhenNotInitialized(reject: reject)) { return }
if (playWhenReady) {
configureAudioSession(setActive: true)
}
player.playWhenReady = playWhenReady
resolve(NSNull())
}
Expand Down Expand Up @@ -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

configureAudioSession();
}

var a: Dictionary<String, Any> = ["lastPosition": lastPosition ?? 0]
if let lastIndex = lastIndex {
a["lastIndex"] = lastIndex
Expand Down Expand Up @@ -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

emit(
event: EventType.PlaybackPlayWhenReadyChanged,
body: [
Expand Down