fix: conflict between gif video and audio

This commit is contained in:
sunxiaojian 2021-03-10 17:14:12 +08:00
parent e1143b0ce4
commit 7556e57de9
6 changed files with 86 additions and 51 deletions

View File

@ -168,7 +168,7 @@ extension StatusSection {
// set audio // set audio
if let audioAttachment = mediaAttachments.filter({ $0.type == .audio }).first { if let audioAttachment = mediaAttachments.filter({ $0.type == .audio }).first {
cell.statusView.audioView.isHidden = false cell.statusView.audioView.isHidden = false
AudioContainerViewModel.configure(cell: cell, audioAttachment: audioAttachment) AudioContainerViewModel.configure(cell: cell, audioAttachment: audioAttachment, videoPlaybackService: dependency.context.videoPlaybackService)
} else { } else {
cell.statusView.audioView.isHidden = true cell.statusView.audioView.isHidden = true
} }

View File

@ -5,11 +5,11 @@
// Created by MainasuK Cirno on 2021-3-3. // Created by MainasuK Cirno on 2021-3-3.
// //
import os.log
import UIKit
import Combine import Combine
import CoreDataStack import CoreDataStack
import MastodonSDK import MastodonSDK
import os.log
import UIKit
extension StatusTableViewCellDelegate where Self: StatusProvider { extension StatusTableViewCellDelegate where Self: StatusProvider {
// TODO: // TODO:
@ -29,7 +29,7 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
// not expired AND last update > 60s // not expired AND last update > 60s
guard !poll.expired else { guard !poll.expired else {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s expired. Skip for update", ((#file as NSString).lastPathComponent), #line, #function, poll.id) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s expired. Skip for update", (#file as NSString).lastPathComponent, #line, #function, poll.id)
return nil return nil
} }
let timeIntervalSinceUpdate = now.timeIntervalSince(poll.updatedAt) let timeIntervalSinceUpdate = now.timeIntervalSince(poll.updatedAt)
@ -39,10 +39,10 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
let autoRefreshTimeInterval: TimeInterval = 60 let autoRefreshTimeInterval: TimeInterval = 60
#endif #endif
guard timeIntervalSinceUpdate > autoRefreshTimeInterval else { guard timeIntervalSinceUpdate > autoRefreshTimeInterval else {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s updated in the %.2fs. Skip for update", ((#file as NSString).lastPathComponent), #line, #function, poll.id, timeIntervalSinceUpdate) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s updated in the %.2fs. Skip for update", (#file as NSString).lastPathComponent, #line, #function, poll.id, timeIntervalSinceUpdate)
return nil return nil
} }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info update…", ((#file as NSString).lastPathComponent), #line, #function, poll.id) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info update…", (#file as NSString).lastPathComponent, #line, #function, poll.id)
return self.context.apiService.poll( return self.context.apiService.poll(
domain: toot.domain, domain: toot.domain,
@ -57,13 +57,13 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
.sink(receiveCompletion: { completion in .sink(receiveCompletion: { completion in
switch completion { switch completion {
case .failure(let error): case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info fail to update: %s", ((#file as NSString).lastPathComponent), #line, #function, pollID ?? "?", error.localizedDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info fail to update: %s", (#file as NSString).lastPathComponent, #line, #function, pollID ?? "?", error.localizedDescription)
case .finished: case .finished:
break break
} }
}, receiveValue: { response in }, receiveValue: { response in
let poll = response.value let poll = response.value
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info updated", ((#file as NSString).lastPathComponent), #line, #function, poll.id) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: poll %s info updated", (#file as NSString).lastPathComponent, #line, #function, poll.id)
}) })
.store(in: &disposeBag) .store(in: &disposeBag)
@ -80,9 +80,21 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
.store(in: &disposeBag) .store(in: &disposeBag)
} }
func handleTableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// os_log("%{public}s[%{public}ld], %{public}s: indexPath %s", ((#file as NSString).lastPathComponent), #line, #function, indexPath.debugDescription)
toot(for: cell, indexPath: indexPath)
.sink { [weak self] toot in
guard let self = self else { return }
guard let media = (toot?.mediaAttachments ?? Set()).first else { return }
guard let videoPlayerViewModel = self.context.videoPlaybackService.dequeueVideoPlayerViewModel(for: media) else { return }
DispatchQueue.main.async {
videoPlayerViewModel.didEndDisplaying()
}
}
.store(in: &disposeBag)
}
} }
extension StatusTableViewCellDelegate where Self: StatusProvider { extension StatusTableViewCellDelegate where Self: StatusProvider {}
}

View File

@ -12,7 +12,8 @@ import UIKit
class AudioContainerViewModel { class AudioContainerViewModel {
static func configure( static func configure(
cell: StatusTableViewCell, cell: StatusTableViewCell,
audioAttachment: Attachment audioAttachment: Attachment,
videoPlaybackService: VideoPlaybackService
) { ) {
guard let duration = audioAttachment.meta?.original?.duration else { return } guard let duration = audioAttachment.meta?.original?.duration else { return }
let audioView = cell.statusView.audioView let audioView = cell.statusView.audioView
@ -25,12 +26,15 @@ class AudioContainerViewModel {
AudioPlayer.shared.pause() AudioPlayer.shared.pause()
} else { } else {
AudioPlayer.shared.resume() AudioPlayer.shared.resume()
videoPlaybackService.pauseWhenPlayAudio()
} }
if AudioPlayer.shared.currentTimeSubject.value == 0 { if AudioPlayer.shared.currentTimeSubject.value == 0 {
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment) AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
videoPlaybackService.pauseWhenPlayAudio()
} }
} else { } else {
AudioPlayer.shared.playAudio(audioAttachment: audioAttachment) AudioPlayer.shared.playAudio(audioAttachment: audioAttachment)
videoPlaybackService.pauseWhenPlayAudio()
} }
} }
.store(in: &cell.disposeBag) .store(in: &cell.disposeBag)
@ -41,7 +45,7 @@ class AudioContainerViewModel {
AudioPlayer.shared.seekToTime(time: time) AudioPlayer.shared.seekToTime(time: time)
} }
.store(in: &cell.disposeBag) .store(in: &cell.disposeBag)
self.observePlayer(cell: cell, audioAttachment: audioAttachment) observePlayer(cell: cell, audioAttachment: audioAttachment)
if audioAttachment != AudioPlayer.shared.attachment { if audioAttachment != AudioPlayer.shared.attachment {
configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped) configureAudioView(audioView: audioView, audioAttachment: audioAttachment, playbackState: .stopped)
} }

View File

@ -5,14 +5,13 @@
// Created by xiaojian sun on 2021/3/10. // Created by xiaojian sun on 2021/3/10.
// //
import AVKit
import Combine
import CoreDataStack
import os.log import os.log
import UIKit import UIKit
import AVKit
import CoreDataStack
import Combine
final class VideoPlayerViewModel { final class VideoPlayerViewModel {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
// input // input
@ -55,7 +54,7 @@ final class VideoPlayerViewModel {
timeControlStatusObservation = player.observe(\.timeControlStatus, options: [.initial, .new]) { [weak self] player, _ in timeControlStatusObservation = player.observe(\.timeControlStatus, options: [.initial, .new]) { [weak self] player, _ in
guard let self = self else { return } guard let self = self else { return }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: player state: %s", ((#file as NSString).lastPathComponent), #line, #function, player.timeControlStatus.debugDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: player state: %s", (#file as NSString).lastPathComponent, #line, #function, player.timeControlStatus.debugDescription)
self.timeControlStatus.value = player.timeControlStatus self.timeControlStatus.value = player.timeControlStatus
} }
@ -64,11 +63,13 @@ final class VideoPlayerViewModel {
.sink { [weak self] timeControlStatus in .sink { [weak self] timeControlStatus in
guard let _ = self else { return } guard let _ = self else { return }
guard timeControlStatus == .playing else { return } guard timeControlStatus == .playing else { return }
AudioPlayer.shared.pauseIfNeed()
switch videoKind { switch videoKind {
case .gif: try? AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default) case .gif:
case .video: try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default) break
case .video:
try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default)
} }
try? AVAudioSession.sharedInstance().setActive(true)
} }
.store(in: &disposeBag) .store(in: &disposeBag)
@ -84,7 +85,6 @@ final class VideoPlayerViewModel {
deinit { deinit {
timeControlStatusObservation = nil timeControlStatusObservation = nil
} }
} }
extension VideoPlayerViewModel { extension VideoPlayerViewModel {
@ -95,7 +95,6 @@ extension VideoPlayerViewModel {
} }
extension VideoPlayerViewModel { extension VideoPlayerViewModel {
func setupLooper() { func setupLooper() {
guard looper == nil, let queuePlayer = player as? AVQueuePlayer else { return } guard looper == nil, let queuePlayer = player as? AVQueuePlayer else { return }
guard let templateItem = queuePlayer.items().first else { return } guard let templateItem = queuePlayer.items().first else { return }
@ -104,10 +103,12 @@ extension VideoPlayerViewModel {
func play() { func play() {
switch videoKind { switch videoKind {
case .gif: try? AVAudioSession.sharedInstance().setCategory(.ambient, mode: .default) case .gif:
case .video: try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default) break
case .video:
try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default)
} }
try? AVAudioSession.sharedInstance().setActive(true)
player.play() player.play()
updateDate = Date() updateDate = Date()
} }
@ -118,7 +119,7 @@ extension VideoPlayerViewModel {
} }
func willDisplay() { func willDisplay() {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: url: %s", ((#file as NSString).lastPathComponent), #line, #function, videoURL.debugDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: url: %s", (#file as NSString).lastPathComponent, #line, #function, videoURL.debugDescription)
switch videoKind { switch videoKind {
case .gif: case .gif:
@ -136,7 +137,7 @@ extension VideoPlayerViewModel {
} }
func didEndDisplaying() { func didEndDisplaying() {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: url: %s", ((#file as NSString).lastPathComponent), #line, #function, videoURL.debugDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: url: %s", (#file as NSString).lastPathComponent, #line, #function, videoURL.debugDescription)
isPlayingWhenEndDisplaying = timeControlStatus.value != .paused isPlayingWhenEndDisplaying = timeControlStatus.value != .paused
switch videoKind { switch videoKind {
@ -148,5 +149,4 @@ extension VideoPlayerViewModel {
updateDate = Date() updateDate = Date()
} }
} }

View File

@ -111,6 +111,12 @@ extension AudioPlayer {
self.currentTimeSubject.value = 0 self.currentTimeSubject.value = 0
} }
.store(in: &disposeBag) .store(in: &disposeBag)
NotificationCenter.default.publisher(for: AVAudioSession.interruptionNotification, object: nil)
.sink { [weak self] _ in
guard let self = self else { return }
self.pause()
}
.store(in: &disposeBag)
} }
func isPlaying() -> Bool { func isPlaying() -> Bool {
@ -125,7 +131,11 @@ extension AudioPlayer {
player.pause() player.pause()
playbackState.value = .paused playbackState.value = .paused
} }
func pauseIfNeed() {
if isPlaying() {
pause()
}
}
func seekToTime(time: TimeInterval) { func seekToTime(time: TimeInterval) {
player.seek(to: CMTimeMake(value:Int64(time), timescale: 1)) player.seek(to: CMTimeMake(value:Int64(time), timescale: 1))
} }

View File

@ -5,14 +5,13 @@
// Created by xiaojian sun on 2021/3/10. // Created by xiaojian sun on 2021/3/10.
// //
import os.log
import Foundation
import AVKit import AVKit
import Combine import Combine
import CoreDataStack import CoreDataStack
import Foundation
import os.log
final class VideoPlaybackService { final class VideoPlaybackService {
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
let workingQueue = DispatchQueue(label: "com.twidere.twiderex.video-playback-service.working-queue") let workingQueue = DispatchQueue(label: "com.twidere.twiderex.video-playback-service.working-queue")
@ -20,7 +19,6 @@ final class VideoPlaybackService {
// only for video kind // only for video kind
weak var latestPlayingVideoPlayerViewModel: VideoPlayerViewModel? weak var latestPlayingVideoPlayerViewModel: VideoPlayerViewModel?
} }
extension VideoPlaybackService { extension VideoPlaybackService {
@ -43,7 +41,6 @@ extension VideoPlaybackService {
if latestPlayingVideoPlayerViewModel === playerViewModel { if latestPlayingVideoPlayerViewModel === playerViewModel {
latestPlayingVideoPlayerViewModel = nil latestPlayingVideoPlayerViewModel = nil
try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default) try? AVAudioSession.sharedInstance().setCategory(.soloAmbient, mode: .default)
try? AVAudioSession.sharedInstance().setActive(false)
} }
} }
} }
@ -51,16 +48,15 @@ extension VideoPlaybackService {
} }
extension VideoPlaybackService { extension VideoPlaybackService {
func dequeueVideoPlayerViewModel(for media: Attachment) -> VideoPlayerViewModel? { func dequeueVideoPlayerViewModel(for media: Attachment) -> VideoPlayerViewModel? {
// Core Data entity not thread-safe. Save attribute before enter working queue // Core Data entity not thread-safe. Save attribute before enter working queue
guard let height = media.meta?.original?.height, guard let height = media.meta?.original?.height,
let width = media.meta?.original?.width, let width = media.meta?.original?.width,
let url = URL(string: media.url), let url = URL(string: media.url),
media.type == .gifv || media.type == .video else media.type == .gifv || media.type == .video
{ return nil } else { return nil }
let previewImageURL = media.previewURL.flatMap({ URL(string: $0) }) let previewImageURL = media.previewURL.flatMap { URL(string: $0) }
let videoKind: VideoPlayerViewModel.Kind = media.type == .gifv ? .gif : .video let videoKind: VideoPlayerViewModel.Kind = media.type == .gifv ? .gif : .video
var _viewModel: VideoPlayerViewModel? var _viewModel: VideoPlayerViewModel?
@ -95,7 +91,6 @@ extension VideoPlaybackService {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
} }
} }
extension VideoPlaybackService { extension VideoPlaybackService {
@ -106,7 +101,7 @@ extension VideoPlaybackService {
} }
func viewDidDisappear(from viewController: UIViewController?) { func viewDidDisappear(from viewController: UIViewController?) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", (#file as NSString).lastPathComponent, #line, #function)
// note: do not retain view controller // note: do not retain view controller
// pause all player when view disppear exclude full screen player and other transitioning scene // pause all player when view disppear exclude full screen player and other transitioning scene
@ -116,11 +111,25 @@ extension VideoPlaybackService {
continue continue
} }
guard !viewModel.isFullScreenPresentationing else { guard !viewModel.isFullScreenPresentationing else {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: isFullScreenPresentationing", ((#file as NSString).lastPathComponent), #line, #function) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: isFullScreenPresentationing", (#file as NSString).lastPathComponent, #line, #function)
continue continue
} }
guard viewModel.videoKind == .video else { continue } guard viewModel.videoKind == .video else { continue }
viewModel.pause() viewModel.pause()
} }
} }
func pauseWhenPlayAudio() {
for viewModel in viewPlayerViewModelDict.values {
guard !viewModel.isTransitioning else {
viewModel.isTransitioning = false
continue
}
guard !viewModel.isFullScreenPresentationing else {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: isFullScreenPresentationing", (#file as NSString).lastPathComponent, #line, #function)
continue
}
viewModel.pause()
}
}
} }