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.
This commit is contained in:
Jackson Harper 2022-09-15 13:42:44 +08:00
parent f6a3b7091c
commit 460ba07e30

View file

@ -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)