mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-26 13:24:39 +02:00
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:
@@ -424,6 +424,27 @@ class AudioPlayer: NSObject {
|
|||||||
let indexOfSeek = getItemIndexForTime(time: to)
|
let indexOfSeek = getItemIndexForTime(time: to)
|
||||||
logger.log("SEEK: Seek to index \(indexOfSeek) | Current index \(self.currentTrackIndex)")
|
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
|
// Reconstruct queue if seeking to a different track
|
||||||
if (self.currentTrackIndex != indexOfSeek) {
|
if (self.currentTrackIndex != indexOfSeek) {
|
||||||
// When we seek to a different track, we need to make sure to seek the old track to 0
|
// 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? {
|
public func getCurrentTime() -> Double? {
|
||||||
guard let playbackSession = self.getPlaybackSession() else { return nil }
|
guard let playbackSession = self.getPlaybackSession() else { return nil }
|
||||||
let currentTrackTime = self.audioPlayer.currentTime().seconds
|
|
||||||
let audioTrack = playbackSession.audioTracks[currentTrackIndex]
|
let audioTrack = playbackSession.audioTracks[currentTrackIndex]
|
||||||
let startOffset = audioTrack.startOffset ?? 0.0
|
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
|
return startOffset + currentTrackTime
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -768,6 +794,11 @@ class AudioPlayer: NSObject {
|
|||||||
if keyPath == #keyPath(AVPlayer.currentItem) {
|
if keyPath == #keyPath(AVPlayer.currentItem) {
|
||||||
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
|
NotificationCenter.default.post(name: NSNotification.Name(PlayerEvents.update.rawValue), object: nil)
|
||||||
logger.log("WARNING: Item ended")
|
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 {
|
} else {
|
||||||
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
||||||
|
|||||||
Reference in New Issue
Block a user