-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: adds iosCategory update functionality #2259
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -140,7 +140,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |
| private func rejectWhenNotInitialized(reject: RCTPromiseRejectBlock) -> Bool { | ||
| let rejected = !hasInitialized; | ||
| if (rejected) { | ||
| reject("player_not_initialized", "The player is not initialized. Call setupPlayer first.", nil) | ||
| reject("player_already_initialized", "The player is not initialized. Call setupPlayer first.", nil) | ||
| } | ||
| return rejected; | ||
| } | ||
|
|
@@ -183,28 +183,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |
| // configure wether control center metdata should auto update | ||
| player.automaticallyUpdateNowPlayingInfo = config["autoUpdateMetadata"] as? Bool ?? true | ||
|
|
||
| // configure audio session - category, options & mode | ||
| if | ||
| let sessionCategoryStr = config["iosCategory"] as? String, | ||
| let mappedCategory = SessionCategory(rawValue: sessionCategoryStr) { | ||
| sessionCategory = mappedCategory.mapConfigToAVAudioSessionCategory() | ||
| } | ||
|
|
||
| if | ||
| let sessionCategoryModeStr = config["iosCategoryMode"] as? String, | ||
| let mappedCategoryMode = SessionCategoryMode(rawValue: sessionCategoryModeStr) { | ||
| sessionCategoryMode = mappedCategoryMode.mapConfigToAVAudioSessionCategoryMode() | ||
| } | ||
|
|
||
| if | ||
| let sessionCategoryPolicyStr = config["iosCategoryPolicy"] as? String, | ||
| let mappedCategoryPolicy = SessionCategoryPolicy(rawValue: sessionCategoryPolicyStr) { | ||
| sessionCategoryPolicy = mappedCategoryPolicy.mapConfigToAVAudioSessionCategoryPolicy() | ||
| } | ||
|
|
||
| let sessionCategoryOptsStr = config["iosCategoryOptions"] as? [String] | ||
| let mappedCategoryOpts = sessionCategoryOptsStr?.compactMap { SessionCategoryOptions(rawValue: $0)?.mapConfigToAVAudioSessionCategoryOptions() } ?? [] | ||
| sessionCategoryOptions = AVAudioSession.CategoryOptions(mappedCategoryOpts) | ||
| updateCategory(config: config) | ||
|
|
||
| configureAudioSession() | ||
|
|
||
|
|
@@ -291,6 +270,30 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |
| resolve(NSNull()) | ||
| } | ||
|
|
||
| private func updateCategory(config: [String: Any]) { | ||
| // configure audio session - category, options & mode | ||
| if | ||
| let sessionCategoryStr = config["iosCategory"] as? String, | ||
| let mappedCategory = SessionCategory(rawValue: sessionCategoryStr) { | ||
| sessionCategory = mappedCategory.mapConfigToAVAudioSessionCategory() | ||
| } | ||
|
|
||
| if | ||
| let sessionCategoryModeStr = config["iosCategoryMode"] as? String, | ||
| let mappedCategoryMode = SessionCategoryMode(rawValue: sessionCategoryModeStr) { | ||
| sessionCategoryMode = mappedCategoryMode.mapConfigToAVAudioSessionCategoryMode() | ||
| } | ||
|
|
||
| if | ||
| let sessionCategoryPolicyStr = config["iosCategoryPolicy"] as? String, | ||
| let mappedCategoryPolicy = SessionCategoryPolicy(rawValue: sessionCategoryPolicyStr) { | ||
| sessionCategoryPolicy = mappedCategoryPolicy.mapConfigToAVAudioSessionCategoryPolicy() | ||
| } | ||
|
|
||
| let sessionCategoryOptsStr = config["iosCategoryOptions"] as? [String] | ||
| let mappedCategoryOpts = sessionCategoryOptsStr?.compactMap { SessionCategoryOptions(rawValue: $0)?.mapConfigToAVAudioSessionCategoryOptions() } ?? [] | ||
| sessionCategoryOptions = AVAudioSession.CategoryOptions(mappedCategoryOpts) | ||
| } | ||
|
|
||
| private func configureAudioSession() { | ||
|
|
||
|
|
@@ -346,6 +349,10 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate { | |
| interval: ((options["progressUpdateEventInterval"] as? NSNumber) ?? 0).doubleValue | ||
| ) | ||
|
|
||
| updateCategory(config: options) | ||
|
|
||
| configureAudioSession() | ||
|
Author
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. @dcvz Can I call this here safely? I realize that reinitialization is something that should be avoided based on this code inside 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. I think you can update this safely since the phone switches nicely between the two speakers when updating the iosCategory and iosCategoryMode properties. |
||
|
|
||
| resolve(NSNull()) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.