This commit is contained in:
Thomas Ricouard 2022-12-24 08:29:45 +01:00
parent 25a80eea65
commit a90a63bf1b
2 changed files with 38 additions and 44 deletions

View File

@ -0,0 +1,37 @@
import SwiftUI
import AVKit
class VideoPlayerViewModel: ObservableObject {
@Published var player: AVPlayer?
private let url: URL
init(url: URL) {
self.url = url
}
func preparePlayer() {
player = .init(url: url)
player?.play()
guard let player else { return }
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
object: player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: CMTime.zero)
self?.player?.play()
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: self.player)
}
}
struct VideoPlayerView: View {
@StateObject var viewModel: VideoPlayerViewModel
var body: some View {
VStack {
VideoPlayer(player: viewModel.player)
}.onAppear {
viewModel.preparePlayer()
}
}
}

View File

@ -1,56 +1,13 @@
import SwiftUI
import Models
import AVKit
import Env
import Shimmer
private class VideoPlayerViewModel: ObservableObject {
@Published var player: AVPlayer?
private let url: URL
init(url: URL) {
self.url = url
}
func preparePlayer() {
player = .init(url: url)
player?.play()
guard let player else { return }
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
object: player.currentItem, queue: .main) { [weak self] _ in
self?.player?.seek(to: CMTime.zero)
self?.player?.play()
}
}
deinit {
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: self.player)
}
}
private struct VideoPlayerView: View {
@StateObject var viewModel: VideoPlayerViewModel
var body: some View {
VStack {
VideoPlayer(player: viewModel.player)
}.onAppear {
viewModel.preparePlayer()
}
}
}
// Could have just been a state, but SwiftUI .sheet is buggy ATM without @StateObject
private class SelectedMediaSheetManager: ObservableObject {
@Published var selectedAttachement: MediaAttachement?
}
public struct StatusMediaPreviewView: View {
@EnvironmentObject private var quickLook: QuickLook
public let attachements: [MediaAttachement]
@StateObject private var selectedMediaSheetManager = SelectedMediaSheetManager()
@State private var isQuickLookLoading: Bool = false
private var imageMaxHeight: CGFloat {