Simplify scrubbing logic.

This commit is contained in:
David Langley
2021-09-22 16:26:20 +01:00
parent 80f407857f
commit fb5b1b28e9
4 changed files with 23 additions and 40 deletions
@@ -102,11 +102,6 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
return
}
guard progress > 0 else {
audioPlayer.stop()
return
}
if audioPlayer.url == nil,
let url = urlToLoad {
audioPlayer.loadContentFromURL(url, displayName: attachment?.originalFileName)
@@ -114,9 +109,6 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
audioPlayer.seekToTime(self.duration * Double(progress)) { [weak self] _ in
guard let self = self else { return }
if self.state == .stopped {
self.state = .paused
}
self.updateUI()
}
}
@@ -149,8 +141,10 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
}
func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
audioPlayer.seekToTime(0.0)
state = .stopped
audioPlayer.seekToTime(0.0) { [weak self] _ in
guard let self = self else { return }
self.state = .stopped
}
}
// MARK: - Private
@@ -165,20 +159,13 @@ class VoiceMessagePlaybackController: VoiceMessageAudioPlayerDelegate, VoiceMess
details.playbackEnabled = (state != .error)
details.playing = (state == .playing)
details.samples = samples
switch state {
case .stopped:
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: self.duration))
details.progress = 0.0
default:
if let audioPlayer = audioPlayer {
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: audioPlayer.currentTime))
details.progress = (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0)
}
}
// Show the current time if the player is paused, show duration when at 0.
let duration = self.duration
let currentTime = audioPlayer?.currentTime ?? 0
let displayTime = currentTime > 0 ? currentTime : duration
details.currentTime = VoiceMessagePlaybackController.timeFormatter.string(from: Date(timeIntervalSinceReferenceDate: displayTime))
details.progress = duration > 0 ? currentTime / duration : 0
details.loading = self.loading
playbackView.configureWithDetails(details)
}