-
-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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() | ||
} | ||
} | ||
|
||
|
@@ -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()) | ||
} | ||
|
@@ -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()) | ||
} | ||
|
@@ -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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
configureAudioSession(); | ||
} | ||
|
||
var a: Dictionary<String, Any> = ["lastPosition": lastPosition ?? 0] | ||
if let lastIndex = lastIndex { | ||
a["lastIndex"] = lastIndex | ||
|
@@ -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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. we are now checking for |
||
emit( | ||
event: EventType.PlaybackPlayWhenReadyChanged, | ||
body: [ | ||
|
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"
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