Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
3 changes: 3 additions & 0 deletions docs/docs/api/objects/update-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ All parameters are optional. You also only need to specify the ones you want to
| `forwardIcon` | [Resource Object](../objects/resource.md) | The jump forward icon¹ | ✅ | ❌ | ❌ |
| `color` | `number` | The notification color in an ARGB hex | ✅ | ❌ | ❌ |
| `progressUpdateEventInterval` | `number` | The interval (in seconds) that the [`Event.PlaybackProgressUpdated`](../events.md#playbackprogressupdated) will be fired. `undefined` by default. | ✅ | ✅ | ❌ |
| options.iosCategory | `IOSCategory` | [AVAudioSession.Category](https://developer.apple.com/documentation/avfoundation/avaudiosession/1616615-category) for iOS. Sets on `play()` | `IOSCategory.Playback` | ❌ | ✅ | ❌ |
| options.iosCategoryOptions | `IOSCategoryOptions[]` | [AVAudioSession.CategoryOptions](https://developer.apple.com/documentation/avfoundation/avaudiosession/1616503-categoryoptions) for iOS. Sets on `play()` | `[]` | ❌ | ✅ | ❌ |
| options.iosCategoryMode | `IOSCategoryMode` | [AVAudioSession.Mode](https://developer.apple.com/documentation/avfoundation/avaudiosession/1616508-mode) for iOS. Sets on `play()` | `default` | ❌ | ✅ | ❌ |

*¹ - The custom icons will only work in release builds*
53 changes: 30 additions & 23 deletions ios/RNTrackPlayer/RNTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
watadarkstar marked this conversation as resolved.
Outdated
}
return rejected;
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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() {

Expand Down Expand Up @@ -346,6 +349,10 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
interval: ((options["progressUpdateEventInterval"] as? NSNumber) ?? 0).doubleValue
)

updateCategory(config: options)

configureAudioSession()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 setupPlayer:

            reject("player_already_initialized", "The player has already been initialized via setupPlayer.", nil)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Expand Down