2021-03-08 04:42:10 +01:00
|
|
|
//
|
|
|
|
// AudioPlayer.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/8.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AVFoundation
|
|
|
|
import Combine
|
|
|
|
import CoreDataStack
|
|
|
|
import Foundation
|
|
|
|
import UIKit
|
2021-03-11 06:11:13 +01:00
|
|
|
import os.log
|
2021-03-08 04:42:10 +01:00
|
|
|
|
2021-03-11 06:11:13 +01:00
|
|
|
final class AudioPlaybackService: NSObject {
|
2021-03-10 14:19:56 +01:00
|
|
|
|
2021-06-11 22:37:54 +02:00
|
|
|
static let appWillPlayAudioNotification = NSNotification.Name(rawValue: "org.joinmastodon.app.audio-playback-service.appWillPlayAudio")
|
2021-03-10 14:19:56 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
var player = AVPlayer()
|
|
|
|
var timeObserver: Any?
|
|
|
|
var statusObserver: Any?
|
|
|
|
var attachment: Attachment?
|
2021-03-09 11:54:21 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
let playbackState = CurrentValueSubject<PlaybackState, Never>(PlaybackState.unknown)
|
|
|
|
|
|
|
|
let currentTimeSubject = CurrentValueSubject<TimeInterval, Never>(0)
|
|
|
|
|
2021-03-11 06:11:13 +01:00
|
|
|
override init() {
|
2021-03-08 04:42:10 +01:00
|
|
|
super.init()
|
|
|
|
addObserver()
|
2021-06-30 11:56:31 +02:00
|
|
|
|
|
|
|
playbackState
|
|
|
|
.receive(on: RunLoop.main)
|
|
|
|
.sink { status in
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: audio status: %s", ((#file as NSString).lastPathComponent), #line, #function, status.description)
|
|
|
|
switch status {
|
|
|
|
case .unknown, .buffering, .readyToPlay:
|
|
|
|
break
|
|
|
|
case .playing:
|
2021-08-05 12:16:50 +02:00
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.playback)
|
2021-06-30 11:56:31 +02:00
|
|
|
try? AVAudioSession.sharedInstance().setActive(true)
|
|
|
|
case .paused, .stopped, .failed:
|
2021-08-05 12:16:50 +02:00
|
|
|
try? AVAudioSession.sharedInstance().setCategory(.soloAmbient) // reset to default
|
2021-06-30 11:56:31 +02:00
|
|
|
try? AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 06:11:13 +01:00
|
|
|
extension AudioPlaybackService {
|
2021-03-08 04:42:10 +01:00
|
|
|
func playAudio(audioAttachment: Attachment) {
|
|
|
|
guard let url = URL(string: audioAttachment.url) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-11 06:11:13 +01:00
|
|
|
notifyWillPlayAudioNotification()
|
2021-03-08 04:42:10 +01:00
|
|
|
if audioAttachment == attachment {
|
2021-03-09 09:54:05 +01:00
|
|
|
if self.playbackState.value == .stopped {
|
2021-03-09 11:54:21 +01:00
|
|
|
self.seekToTime(time: .zero)
|
2021-03-09 09:54:05 +01:00
|
|
|
}
|
2021-03-08 04:42:10 +01:00
|
|
|
player.play()
|
2021-03-09 09:54:05 +01:00
|
|
|
self.playbackState.value = .playing
|
2021-03-08 04:42:10 +01:00
|
|
|
return
|
|
|
|
}
|
2021-03-09 10:13:17 +01:00
|
|
|
player.pause()
|
2021-03-08 04:42:10 +01:00
|
|
|
let playerItem = AVPlayerItem(url: url)
|
|
|
|
player.replaceCurrentItem(with: playerItem)
|
|
|
|
attachment = audioAttachment
|
|
|
|
player.play()
|
2021-03-09 09:54:05 +01:00
|
|
|
playbackState.value = .playing
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func addObserver() {
|
2021-03-10 14:19:56 +01:00
|
|
|
NotificationCenter.default.publisher(for: VideoPlayerViewModel.appWillPlayVideoNotification)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.pauseIfNeed()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-03-09 08:18:36 +01:00
|
|
|
|
2021-03-08 04:42:10 +01:00
|
|
|
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
|
|
|
|
})
|
2021-06-30 11:56:31 +02:00
|
|
|
player.publisher(for: \.status, options: [.initial, .new])
|
2021-03-09 08:18:36 +01:00
|
|
|
.sink(receiveValue: { [weak self] status in
|
|
|
|
guard let self = self else { return }
|
2021-03-08 04:42:10 +01:00
|
|
|
switch status {
|
|
|
|
case .failed:
|
|
|
|
self.playbackState.value = .failed
|
|
|
|
case .readyToPlay:
|
|
|
|
self.playbackState.value = .readyToPlay
|
|
|
|
case .unknown:
|
|
|
|
self.playbackState.value = .unknown
|
|
|
|
@unknown default:
|
2021-03-09 11:54:21 +01:00
|
|
|
assertionFailure()
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.store(in: &disposeBag)
|
2021-03-09 08:18:36 +01:00
|
|
|
NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime, object: nil)
|
2021-03-09 11:54:21 +01:00
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.player.seek(to: .zero)
|
2021-03-09 09:54:05 +01:00
|
|
|
self.playbackState.value = .stopped
|
|
|
|
self.currentTimeSubject.value = 0
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
2021-03-09 08:18:36 +01:00
|
|
|
.store(in: &disposeBag)
|
2021-03-10 10:14:12 +01:00
|
|
|
NotificationCenter.default.publisher(for: AVAudioSession.interruptionNotification, object: nil)
|
|
|
|
.sink { [weak self] _ in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.pause()
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
|
2021-03-11 06:11:13 +01:00
|
|
|
func notifyWillPlayAudioNotification() {
|
|
|
|
NotificationCenter.default.post(name: AudioPlaybackService.appWillPlayAudioNotification, object: nil)
|
2021-03-10 14:19:56 +01:00
|
|
|
}
|
2021-03-09 09:54:05 +01:00
|
|
|
func isPlaying() -> Bool {
|
2021-03-10 14:19:56 +01:00
|
|
|
return playbackState.value == .readyToPlay || playbackState.value == .playing
|
2021-03-09 09:54:05 +01:00
|
|
|
}
|
2021-03-08 04:42:10 +01:00
|
|
|
func resume() {
|
2021-03-11 06:11:13 +01:00
|
|
|
notifyWillPlayAudioNotification()
|
2021-03-08 04:42:10 +01:00
|
|
|
player.play()
|
2021-03-09 09:54:05 +01:00
|
|
|
playbackState.value = .playing
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pause() {
|
|
|
|
player.pause()
|
2021-03-09 09:54:05 +01:00
|
|
|
playbackState.value = .paused
|
2021-03-08 04:42:10 +01:00
|
|
|
}
|
2021-03-10 10:14:12 +01:00
|
|
|
func pauseIfNeed() {
|
|
|
|
if isPlaying() {
|
|
|
|
pause()
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 04:42:10 +01:00
|
|
|
func seekToTime(time: TimeInterval) {
|
|
|
|
player.seek(to: CMTimeMake(value:Int64(time), timescale: 1))
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 06:11:13 +01:00
|
|
|
|
|
|
|
extension AudioPlaybackService {
|
|
|
|
func viewDidDisappear(from viewController: UIViewController?) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
|
|
|
|
pause()
|
|
|
|
}
|
|
|
|
}
|