From 460ba07e3098e1f2c652b8b511fe7a38614af927 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 15 Sep 2022 13:42:44 +0800 Subject: [PATCH] Update UI after seeking, dont try to detect end of stream using currentTime This helps with two issues: - after seeking sometimes you will see the loading spinner even after audio has resumed. - sometimes if we are still pulling data we might detect that we are at the end of the stream. --- .../Services/AudioSession/AudioController.swift | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index 17fc4e9e4..6382da81c 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -212,7 +212,6 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate // within this index, but this is probably accurate enough for now. player?.removeAllItems() synthesizeFrom(start: foundIdx, playWhenReady: state == .playing, atOffset: remainder) - return } else { // There was no foundIdx, so we are probably trying to seek past the end, so // just seek to the last possible duration. @@ -221,6 +220,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate synthesizeFrom(start: durations.count - 1, playWhenReady: state == .playing, atOffset: last) } } + fireTimer() } @AppStorage(UserDefaultKey.textToSpeechPlaybackRate.rawValue) public var playbackRate = 1.0 { @@ -299,8 +299,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate if state == .reachedEnd { return false } - return - itemAudioProperties?.itemID == itemID && + return itemAudioProperties?.itemID == itemID && (state == .loading || player?.currentItem == nil || player?.currentItem?.status == .unknown) } @@ -431,7 +430,6 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate func startTimer() { if timer == nil { - // Update every 100ms timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(fireTimer), userInfo: nil, repeats: true) timer?.fire() } @@ -445,13 +443,6 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate stop() } - if player.items().count == 1, let currentTime = player.currentItem?.currentTime(), let duration = player.currentItem?.duration { - if currentTime >= duration { - pause() - state = .reachedEnd - } - } - if let durations = durations { duration = durations.reduce(0, +) durationString = formatTimeInterval(duration)