fix: slider not smooth for audio playback issue

This commit is contained in:
CMK 2021-06-17 18:21:49 +08:00
parent 7e1571a493
commit b52508dd03
2 changed files with 10 additions and 7 deletions

View File

@ -52,6 +52,7 @@ final class AudioContainerView: UIView {
let slider: UISlider = {
let slider = UISlider()
slider.isContinuous = true
slider.translatesAutoresizingMaskIntoConstraints = false
slider.minimumTrackTintColor = Asset.Colors.Slider.bar.color
slider.maximumTrackTintColor = Asset.Colors.Slider.bar.color

View File

@ -10,6 +10,7 @@ import Foundation
import UIKit
class AudioContainerViewModel {
static func configure(
cell: StatusCell,
audioAttachment: Attachment,
@ -36,11 +37,12 @@ class AudioContainerViewModel {
}
}
.store(in: &cell.disposeBag)
audioView.slider.maximumValue = Float(duration)
audioView.slider.publisher(for: .valueChanged)
.sink { [weak audioService] slider in
guard let audioService = audioService else { return }
let slider = slider as! UISlider
let time = Double(slider.value) * duration
let time = TimeInterval(slider.value)
audioService.seekToTime(time: time)
}
.store(in: &cell.disposeBag)
@ -58,24 +60,24 @@ class AudioContainerViewModel {
let audioView = cell.statusView.audioView
var lastCurrentTimeSubject: TimeInterval?
audioService.currentTimeSubject
.throttle(for: 0.33, scheduler: DispatchQueue.main, latest: true)
.compactMap { [weak audioService] time -> (TimeInterval, Float)? in
.throttle(for: 0.008, scheduler: DispatchQueue.main, latest: true)
.compactMap { [weak audioService] time -> TimeInterval? in
defer {
lastCurrentTimeSubject = time
}
guard audioAttachment === audioService?.attachment else { return nil }
guard let duration = audioAttachment.meta?.original?.duration else { return nil }
// guard let duration = audioAttachment.meta?.original?.duration else { return nil }
if let lastCurrentTimeSubject = lastCurrentTimeSubject, time != 0.0 {
guard abs(time - lastCurrentTimeSubject) < 0.5 else { return nil } // debounce
}
guard !audioView.slider.isTracking else { return nil }
return (time, Float(time / duration))
return TimeInterval(time)
}
.sink(receiveValue: { time, progress in
.sink(receiveValue: { time in
audioView.timeLabel.text = time.asString(style: .positional)
audioView.slider.setValue(progress, animated: true)
audioView.slider.setValue(Float(time), animated: true)
})
.store(in: &cell.disposeBag)
audioService.playbackState