Merge pull request #1514 from complacentsee/ios_seek_issue_452

Bugfix issue 452:  iOS seek issue at end of book
This commit is contained in:
advplyr 2025-03-15 17:31:00 -05:00 committed by GitHub
commit 36fb117f89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -424,6 +424,27 @@ class AudioPlayer: NSObject {
let indexOfSeek = getItemIndexForTime(time: to)
logger.log("SEEK: Seek to index \(indexOfSeek) | Current index \(self.currentTrackIndex)")
if self.audioPlayer.currentItem == nil {
self.currentTrackIndex = indexOfSeek
try? playbackSession.update {
playbackSession.currentTime = to
}
let playerItems = self.allPlayerItems[indexOfSeek..<self.allPlayerItems.count]
DispatchQueue.runOnMainQueue {
self.audioPlayer.removeAllItems()
for item in Array(playerItems) {
self.audioPlayer.insert(item, after:self.audioPlayer.items().last)
}
}
seekInCurrentTrack(to: to, playbackSession: playbackSession)
setupQueueItemStatusObserver()
return
}
// Reconstruct queue if seeking to a different track
if (self.currentTrackIndex != indexOfSeek) {
// When we seek to a different track, we need to make sure to seek the old track to 0
@ -487,9 +508,14 @@ class AudioPlayer: NSObject {
public func getCurrentTime() -> Double? {
guard let playbackSession = self.getPlaybackSession() else { return nil }
let currentTrackTime = self.audioPlayer.currentTime().seconds
let audioTrack = playbackSession.audioTracks[currentTrackIndex]
let startOffset = audioTrack.startOffset ?? 0.0
// if the currentTrackTime isNan, then fall back on session.
let currentTrackTime = self.audioPlayer.currentTime().seconds
if currentTrackTime.isNaN {
return playbackSession.currentTime
}
return startOffset + currentTrackTime
}
@ -768,6 +794,11 @@ class AudioPlayer: NSObject {
if keyPath == #keyPath(AVPlayer.currentItem) {
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
logger.log("WARNING: Item ended")
if audioPlayer.currentItem == nil {
logger.log("Player ended or next item is nil, marking ended")
self.markAudioSessionAs(active: false)
}
}
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)