Always autoplay video in the new media viewer

This commit is contained in:
Thomas Ricouard 2023-10-17 08:24:11 +02:00
parent 1743b3bc08
commit 8ed6d548eb
2 changed files with 8 additions and 6 deletions

View file

@ -8,16 +8,18 @@ import SwiftUI
@Observable public class MediaUIAttachmentVideoViewModel { @Observable public class MediaUIAttachmentVideoViewModel {
var player: AVPlayer? var player: AVPlayer?
private let url: URL private let url: URL
let forceAutoPlay: Bool
public init(url: URL) { public init(url: URL, forceAutoPlay: Bool = false) {
self.url = url self.url = url
self.forceAutoPlay = forceAutoPlay
} }
func preparePlayer(autoPlay: Bool) { func preparePlayer(autoPlay: Bool) {
player = .init(url: url) player = .init(url: url)
player?.isMuted = true player?.isMuted = true
player?.audiovisualBackgroundPlaybackPolicy = .pauses player?.audiovisualBackgroundPlaybackPolicy = .pauses
if autoPlay { if autoPlay || forceAutoPlay {
player?.play() player?.play()
} else { } else {
player?.pause() player?.pause()
@ -27,7 +29,7 @@ import SwiftUI
object: player.currentItem, queue: .main) object: player.currentItem, queue: .main)
{ _ in { _ in
Task { @MainActor [weak self] in Task { @MainActor [weak self] in
if autoPlay { if autoPlay || self?.forceAutoPlay == true {
self?.play() self?.play()
} }
} }
@ -65,7 +67,7 @@ public struct MediaUIAttachmentVideoView: View {
VideoPlayer(player: viewModel.player) VideoPlayer(player: viewModel.player)
.accessibilityAddTraits(.startsMediaSession) .accessibilityAddTraits(.startsMediaSession)
if !preferences.autoPlayVideo { if !preferences.autoPlayVideo && !viewModel.forceAutoPlay {
Image(systemName: "play.fill") Image(systemName: "play.fill")
.font(isCompact ? .body : .largeTitle) .font(isCompact ? .body : .largeTitle)
.foregroundColor(theme.tintColor) .foregroundColor(theme.tintColor)
@ -85,7 +87,7 @@ public struct MediaUIAttachmentVideoView: View {
case .background, .inactive: case .background, .inactive:
viewModel.pause() viewModel.pause()
case .active: case .active:
if preferences.autoPlayVideo { if preferences.autoPlayVideo || viewModel.forceAutoPlay {
viewModel.play() viewModel.play()
} }
default: default:

View file

@ -33,7 +33,7 @@ public struct MediaUIView: View {
.containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0) .containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0)
.id(attachment.id) .id(attachment.id)
case .video, .gifv, .audio: case .video, .gifv, .audio:
MediaUIAttachmentVideoView(viewModel: .init(url: url)) MediaUIAttachmentVideoView(viewModel: .init(url: url, forceAutoPlay: true))
.containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0) .containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0)
.containerRelativeFrame(.vertical, count: 1, span: 1, spacing: 0) .containerRelativeFrame(.vertical, count: 1, span: 1, spacing: 0)
.id(attachment.id) .id(attachment.id)