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

ref(ios, events): improve disabling of playback-progress-updated #1706

Merged
Changes from all 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
19 changes: 9 additions & 10 deletions ios/RNTrackPlayer/RNTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
private var hasInitialized = false
private let player = QueuedAudioPlayer()
private let audioSessionController = AudioSessionController.shared
private var shouldEmitUpdateEventInterval: Bool = false
private var shouldEmitProgressEvent: Bool = false

// MARK: - Lifecycle Methods

Expand Down Expand Up @@ -307,19 +307,18 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
bookmarkOptions: options["bookmarkOptions"] as? [String: Any])
}

if let interval = options["progressUpdateEventInterval"] as? NSNumber, interval.intValue > 0 {
shouldEmitUpdateEventInterval = true
configureProgressUpdateEvent(interval: interval.doubleValue)
} else {
shouldEmitUpdateEventInterval = false
}
configureProgressUpdateEvent(
interval: ((options["progressUpdateEventInterval"] as? NSNumber) ?? 0).doubleValue
)

resolve(NSNull())
}

private func configureProgressUpdateEvent(interval: Double) {
let time = CMTime(seconds: interval, preferredTimescale: 1)
self.player.timeEventFrequency = .custom(time: time)
shouldEmitProgressEvent = interval > 0
self.player.timeEventFrequency = shouldEmitProgressEvent
? .custom(time: CMTime(seconds: interval, preferredTimescale: 1))
: .everySecond
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big difference I see here is that we're setting the interval to 1s when we cancel, which we weren't doing before. Are we sure this is desired?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason I am doing this is that the user might have set the interval to something very small temporarily, expecting that setting it back to 0 would return stuff to normal.. (I was going off the comment that says it is impossible to turn it off..)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@objc(add:before:resolver:rejecter:)
Expand Down Expand Up @@ -820,7 +819,7 @@ public class RNTrackPlayer: RCTEventEmitter, AudioSessionControllerDelegate {
// additionally, there are certain instances in which this event is emitted
// _after_ a manipulation to the queu causing no currentItem to exist (see reset)
// in which case we shouldn't emit anything or we'll get an exception.
if shouldEmitUpdateEventInterval == false || player.currentItem == nil { return }
if !shouldEmitProgressEvent || player.currentItem == nil { return }

sendEvent(
withName: "playback-progress-updated",
Expand Down