-
-
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 all commits
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,23 @@ 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) | ||
} else { | ||
try? AVAudioSession.sharedInstance().setCategory(sessionCategory, mode: sessionCategoryMode, options: sessionCategoryOptions) | ||
} | ||
if (setActive) { | ||
private func configureAudioSession() { | ||
|
||
// deactivate the session when there is no current item to be played | ||
if (player.currentItem == nil) { | ||
try? audioSessionController.deactivateSession() | ||
return | ||
} | ||
|
||
// activate 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) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -521,8 +527,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 +542,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 +861,10 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |
} | ||
} | ||
|
||
if ((item != nil && lastItem == nil) || item == nil) { | ||
configureAudioSession(); | ||
} | ||
|
||
var a: Dictionary<String, Any> = ["lastPosition": lastPosition ?? 0] | ||
if let lastIndex = lastIndex { | ||
a["lastIndex"] = lastIndex | ||
|
@@ -908,6 +913,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.
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 whenplayWhenReady
istrue