Simplify scrubbing logic.

This commit is contained in:
David Langley 2021-09-22 16:26:20 +01:00
parent 3bb50d2a35
commit 442708524e
4 changed files with 23 additions and 40 deletions

View file

@ -122,7 +122,6 @@ class VoiceMessageAudioPlayer: NSObject {
}
func seekToTime(_ time: TimeInterval, completionHandler:@escaping (Bool) -> Void = { _ in }) {
isStopped = false
audioPlayer?.seek(to: CMTime(seconds: time, preferredTimescale: 60000), completionHandler: completionHandler)
}

View file

@ -163,11 +163,6 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
return
}
guard progress > 0 else {
audioPlayer.stop()
return
}
if audioPlayer.url == nil {
audioPlayer.loadContentFromURL(temporaryFileURL)
}
@ -220,8 +215,9 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
}
func audioPlayerDidFinishPlaying(_ audioPlayer: VoiceMessageAudioPlayer) {
audioPlayer.seekToTime(0.0)
updateUI()
audioPlayer.seekToTime(0.0) { [weak self] _ in
self?.updateUI()
}
}
func audioPlayer(_ audioPlayer: VoiceMessageAudioPlayer, didFailWithError: Error) {
@ -428,12 +424,14 @@ public class VoiceMessageController: NSObject, VoiceMessageToolbarViewDelegate,
var details = VoiceMessageToolbarViewDetails()
details.state = (audioRecorder?.isRecording ?? false ? (isInLockedMode ? .lockedModeRecord : .record) : (isInLockedMode ? .lockedModePlayback : .idle))
// Show the current time if the player is paused or playing but not when stopped.
details.elapsedTime = VoiceMessageController.elapsedTimeFormatter.string(from: Date(timeIntervalSinceReferenceDate: (!audioPlayer.isStopped ? audioPlayer.currentTime : audioPlayer.duration)))
// Show the current time if the player is paused, show duration when at 0.
let currentTime = audioPlayer.currentTime
let duration = recordDuration ?? 0
let displayTime = currentTime > 0 ? currentTime : duration
details.elapsedTime = VoiceMessageController.elapsedTimeFormatter.string(from: Date(timeIntervalSinceReferenceDate: displayTime))
details.progress = duration > 0 ? currentTime / duration : 0
details.audioSamples = audioSamples
details.isPlaying = audioPlayer.isPlaying
// Set progress if the player is paused or playing but not when stopped.
details.progress = (!audioPlayer.isStopped ? (audioPlayer.duration > 0.0 ? audioPlayer.currentTime / audioPlayer.duration : 0.0) : 0.0)
_voiceMessageToolbarView.configureWithDetails(details)
}

View file

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

View file

@ -146,17 +146,16 @@ class VoiceMessagePlaybackView: UIView, NibLoadable, Themable {
@IBAction private func tap(gestureRecognizer: UITapGestureRecognizer) {
let x = gestureRecognizer.location(in: waveformContainerView).x.clamped(to: 0...waveformContainerView.bounds.width)
let progress = x / waveformContainerView.bounds.width
let seekPoint = progress == 1 ? 0 : progress
delegate?.voiceMessagePlaybackViewDidRequestSeek(to: seekPoint)
delegate?.voiceMessagePlaybackViewDidRequestSeek(to: progress)
}
@IBAction private func pan(gestureRecognizer: UIPanGestureRecognizer) {
switch gestureRecognizer.state {
case .began, .changed:
let x = gestureRecognizer.location(in: waveformContainerView).x.clamped(to: 0...waveformContainerView.bounds.width)
scrubProgress = x / waveformContainerView.bounds.width
let seekPoint = scrubProgress == 1 ? 0 : scrubProgress ?? 0
delegate?.voiceMessagePlaybackViewDidRequestSeek(to: seekPoint)
let progress = x / waveformContainerView.bounds.width
scrubProgress = progress
delegate?.voiceMessagePlaybackViewDidRequestSeek(to: progress)
default:
scrubProgress = nil
}