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

View file

@ -33,7 +33,7 @@ public struct MediaUIView: View {
.containerRelativeFrame(.horizontal, count: 1, span: 1, spacing: 0)
.id(attachment.id)
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(.vertical, count: 1, span: 1, spacing: 0)
.id(attachment.id)