chore: fix slider shake, reset audioView when stoped

This commit is contained in:
sunxiaojian 2021-03-09 15:18:36 +08:00
parent 30d03a3894
commit 2d4dbad535
3 changed files with 37 additions and 24 deletions

View File

@ -109,6 +109,9 @@ extension AudioContainerView {
container.addArrangedSubview(slider) container.addArrangedSubview(slider)
container.addArrangedSubview(timeLabel) container.addArrangedSubview(timeLabel)
NSLayoutConstraint.activate([
timeLabel.widthAnchor.constraint(equalToConstant: 40),
])
} }
} }

View File

@ -66,12 +66,14 @@ class AudioContainerViewModel {
}) })
.store(in: &cell.disposeBag) .store(in: &cell.disposeBag)
AudioPlayer.shared.playbackState AudioPlayer.shared.playbackState
.map { .receive(on: DispatchQueue.main)
return $0 == .playing || $0 == .readyToPlay .sink(receiveValue: { playbackState in
}
.sink(receiveValue: { isPlaying in
if (audioAttachment === AudioPlayer.shared.attachment) { if (audioAttachment === AudioPlayer.shared.attachment) {
let isPlaying = playbackState == .playing || playbackState == .readyToPlay
audioView.playButton.isSelected = isPlaying audioView.playButton.isSelected = isPlaying
if playbackState == .stopped {
self.resetAudioView(audioView: audioView)
}
} else { } else {
self.resetAudioView(audioView: audioView) self.resetAudioView(audioView: audioView)
} }

View File

@ -57,14 +57,34 @@ extension AudioPlayer {
func addObserver() { func addObserver() {
UIDevice.current.isProximityMonitoringEnabled = true UIDevice.current.isProximityMonitoringEnabled = true
NotificationCenter.default.addObserver(self, selector: #selector(proxumityStateChange), name: UIDevice.proximityStateDidChangeNotification, object: nil) NotificationCenter.default.publisher(for: UIDevice.proximityStateDidChangeNotification, object: nil)
.sink { [weak self] _ in
guard let self = self else { return }
if UIDevice.current.proximityState == true {
do {
try self.session.setCategory(.playAndRecord)
} catch {
print(error)
return
}
} else {
do {
try self.session.setCategory(.playback)
} catch {
print(error)
return
}
}
}
.store(in: &disposeBag)
timeObserver = player.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main, using: { [weak self] time in timeObserver = player.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main, using: { [weak self] time in
guard let self = self else { return } guard let self = self else { return }
self.currentTimeSubject.value = time.seconds self.currentTimeSubject.value = time.seconds
}) })
player.publisher(for: \.status, options: .new) player.publisher(for: \.status, options: .new)
.sink(receiveValue: { status in .sink(receiveValue: { [weak self] status in
guard let self = self else { return }
switch status { switch status {
case .failed: case .failed:
self.playbackState.value = .failed self.playbackState.value = .failed
@ -77,25 +97,13 @@ extension AudioPlayer {
} }
}) })
.store(in: &disposeBag) .store(in: &disposeBag)
NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime, object: nil)
.sink { _ in
self.playbackState.send(PlaybackState.stopped)
}
.store(in: &disposeBag)
} }
@objc func proxumityStateChange(notification: NSNotification) {
if UIDevice.current.proximityState == true {
do {
try session.setCategory(.playAndRecord)
} catch {
print(error)
return
}
} else {
do {
try session.setCategory(.playback)
} catch {
print(error)
return
}
}
}
func resume() { func resume() {
player.play() player.play()