diff --git a/Mastodon/Scene/Share/View/Container/AudioContainerView.swift b/Mastodon/Scene/Share/View/Container/AudioContainerView.swift index cddd7871..e20f5cca 100644 --- a/Mastodon/Scene/Share/View/Container/AudioContainerView.swift +++ b/Mastodon/Scene/Share/View/Container/AudioContainerView.swift @@ -109,6 +109,9 @@ extension AudioContainerView { container.addArrangedSubview(slider) container.addArrangedSubview(timeLabel) + NSLayoutConstraint.activate([ + timeLabel.widthAnchor.constraint(equalToConstant: 40), + ]) } } diff --git a/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift b/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift index ce8d61de..8d4e9e2a 100644 --- a/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift +++ b/Mastodon/Scene/Share/ViewModel/AudioContainerViewModel.swift @@ -66,12 +66,14 @@ class AudioContainerViewModel { }) .store(in: &cell.disposeBag) AudioPlayer.shared.playbackState - .map { - return $0 == .playing || $0 == .readyToPlay - } - .sink(receiveValue: { isPlaying in + .receive(on: DispatchQueue.main) + .sink(receiveValue: { playbackState in if (audioAttachment === AudioPlayer.shared.attachment) { + let isPlaying = playbackState == .playing || playbackState == .readyToPlay audioView.playButton.isSelected = isPlaying + if playbackState == .stopped { + self.resetAudioView(audioView: audioView) + } } else { self.resetAudioView(audioView: audioView) } diff --git a/Mastodon/Service/AudioPlayer.swift b/Mastodon/Service/AudioPlayer.swift index 4a14a080..95be2e78 100644 --- a/Mastodon/Service/AudioPlayer.swift +++ b/Mastodon/Service/AudioPlayer.swift @@ -57,14 +57,34 @@ extension AudioPlayer { func addObserver() { 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 guard let self = self else { return } self.currentTimeSubject.value = time.seconds }) player.publisher(for: \.status, options: .new) - .sink(receiveValue: { status in + .sink(receiveValue: { [weak self] status in + guard let self = self else { return } switch status { case .failed: self.playbackState.value = .failed @@ -77,25 +97,13 @@ extension AudioPlayer { } }) .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() { player.play()