fix: slider not smooth for audio playback issue
This commit is contained in:
parent
7e1571a493
commit
b52508dd03
|
@ -52,6 +52,7 @@ final class AudioContainerView: UIView {
|
||||||
|
|
||||||
let slider: UISlider = {
|
let slider: UISlider = {
|
||||||
let slider = UISlider()
|
let slider = UISlider()
|
||||||
|
slider.isContinuous = true
|
||||||
slider.translatesAutoresizingMaskIntoConstraints = false
|
slider.translatesAutoresizingMaskIntoConstraints = false
|
||||||
slider.minimumTrackTintColor = Asset.Colors.Slider.bar.color
|
slider.minimumTrackTintColor = Asset.Colors.Slider.bar.color
|
||||||
slider.maximumTrackTintColor = Asset.Colors.Slider.bar.color
|
slider.maximumTrackTintColor = Asset.Colors.Slider.bar.color
|
||||||
|
|
|
@ -10,6 +10,7 @@ import Foundation
|
||||||
import UIKit
|
import UIKit
|
||||||
|
|
||||||
class AudioContainerViewModel {
|
class AudioContainerViewModel {
|
||||||
|
|
||||||
static func configure(
|
static func configure(
|
||||||
cell: StatusCell,
|
cell: StatusCell,
|
||||||
audioAttachment: Attachment,
|
audioAttachment: Attachment,
|
||||||
|
@ -36,11 +37,12 @@ class AudioContainerViewModel {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.store(in: &cell.disposeBag)
|
.store(in: &cell.disposeBag)
|
||||||
|
audioView.slider.maximumValue = Float(duration)
|
||||||
audioView.slider.publisher(for: .valueChanged)
|
audioView.slider.publisher(for: .valueChanged)
|
||||||
.sink { [weak audioService] slider in
|
.sink { [weak audioService] slider in
|
||||||
guard let audioService = audioService else { return }
|
guard let audioService = audioService else { return }
|
||||||
let slider = slider as! UISlider
|
let slider = slider as! UISlider
|
||||||
let time = Double(slider.value) * duration
|
let time = TimeInterval(slider.value)
|
||||||
audioService.seekToTime(time: time)
|
audioService.seekToTime(time: time)
|
||||||
}
|
}
|
||||||
.store(in: &cell.disposeBag)
|
.store(in: &cell.disposeBag)
|
||||||
|
@ -58,24 +60,24 @@ class AudioContainerViewModel {
|
||||||
let audioView = cell.statusView.audioView
|
let audioView = cell.statusView.audioView
|
||||||
var lastCurrentTimeSubject: TimeInterval?
|
var lastCurrentTimeSubject: TimeInterval?
|
||||||
audioService.currentTimeSubject
|
audioService.currentTimeSubject
|
||||||
.throttle(for: 0.33, scheduler: DispatchQueue.main, latest: true)
|
.throttle(for: 0.008, scheduler: DispatchQueue.main, latest: true)
|
||||||
.compactMap { [weak audioService] time -> (TimeInterval, Float)? in
|
.compactMap { [weak audioService] time -> TimeInterval? in
|
||||||
defer {
|
defer {
|
||||||
lastCurrentTimeSubject = time
|
lastCurrentTimeSubject = time
|
||||||
}
|
}
|
||||||
guard audioAttachment === audioService?.attachment else { return nil }
|
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 {
|
if let lastCurrentTimeSubject = lastCurrentTimeSubject, time != 0.0 {
|
||||||
guard abs(time - lastCurrentTimeSubject) < 0.5 else { return nil } // debounce
|
guard abs(time - lastCurrentTimeSubject) < 0.5 else { return nil } // debounce
|
||||||
}
|
}
|
||||||
|
|
||||||
guard !audioView.slider.isTracking else { return nil }
|
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.timeLabel.text = time.asString(style: .positional)
|
||||||
audioView.slider.setValue(progress, animated: true)
|
audioView.slider.setValue(Float(time), animated: true)
|
||||||
})
|
})
|
||||||
.store(in: &cell.disposeBag)
|
.store(in: &cell.disposeBag)
|
||||||
audioService.playbackState
|
audioService.playbackState
|
||||||
|
|
Loading…
Reference in New Issue