2022-12-24 07:29:45 +00:00
|
|
|
import AVKit
|
2023-02-19 10:44:18 +00:00
|
|
|
import DesignSystem
|
2023-01-29 09:19:59 +00:00
|
|
|
import Env
|
2023-01-30 06:27:06 +00:00
|
|
|
import SwiftUI
|
2022-12-24 07:29:45 +00:00
|
|
|
|
|
|
|
class VideoPlayerViewModel: ObservableObject {
|
|
|
|
@Published var player: AVPlayer?
|
|
|
|
private let url: URL
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-24 07:29:45 +00:00
|
|
|
init(url: URL) {
|
|
|
|
self.url = url
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2023-01-29 09:19:59 +00:00
|
|
|
func preparePlayer(autoPlay: Bool) {
|
2022-12-24 07:29:45 +00:00
|
|
|
player = .init(url: url)
|
2022-12-30 23:07:42 +00:00
|
|
|
player?.isMuted = true
|
2023-01-29 10:17:43 +00:00
|
|
|
player?.audiovisualBackgroundPlaybackPolicy = .pauses
|
2023-01-29 09:19:59 +00:00
|
|
|
if autoPlay {
|
|
|
|
player?.play()
|
2023-02-04 06:53:03 +00:00
|
|
|
} else {
|
|
|
|
player?.pause()
|
2023-01-29 09:19:59 +00:00
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
guard let player else { return }
|
|
|
|
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime,
|
|
|
|
object: player.currentItem, queue: .main) { [weak self] _ in
|
2023-02-04 06:53:03 +00:00
|
|
|
if autoPlay {
|
|
|
|
self?.player?.seek(to: CMTime.zero)
|
|
|
|
self?.player?.play()
|
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func pause() {
|
|
|
|
player?.pause()
|
|
|
|
}
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2023-01-17 18:41:46 +00:00
|
|
|
func play() {
|
|
|
|
player?.play()
|
|
|
|
}
|
2023-01-17 10:36:01 +00:00
|
|
|
|
2022-12-24 07:29:45 +00:00
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self, name: .AVPlayerItemDidPlayToEndTime, object: self.player)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VideoPlayerView: View {
|
2023-01-17 18:41:46 +00:00
|
|
|
@Environment(\.scenePhase) private var scenePhase
|
2023-01-29 09:19:59 +00:00
|
|
|
@EnvironmentObject private var preferences: UserPreferences
|
2023-02-19 10:44:18 +00:00
|
|
|
@EnvironmentObject private var theme: Theme
|
2023-01-30 06:27:06 +00:00
|
|
|
|
2022-12-24 07:29:45 +00:00
|
|
|
@StateObject var viewModel: VideoPlayerViewModel
|
2023-01-22 05:38:30 +00:00
|
|
|
|
2022-12-24 07:29:45 +00:00
|
|
|
var body: some View {
|
2023-02-19 10:44:18 +00:00
|
|
|
ZStack {
|
2022-12-24 07:29:45 +00:00
|
|
|
VideoPlayer(player: viewModel.player)
|
2023-02-19 10:44:18 +00:00
|
|
|
|
|
|
|
if !preferences.autoPlayVideo {
|
|
|
|
Image(systemName: "play.fill")
|
|
|
|
.font(.largeTitle)
|
|
|
|
.foregroundColor(theme.tintColor)
|
|
|
|
.padding()
|
|
|
|
.background(Circle().fill(.thinMaterial))
|
|
|
|
.padding(theme.statusDisplayStyle == .compact ? 0 : 10)
|
|
|
|
}
|
2022-12-24 07:29:45 +00:00
|
|
|
}.onAppear {
|
2023-01-29 09:19:59 +00:00
|
|
|
viewModel.preparePlayer(autoPlay: preferences.autoPlayVideo)
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
2023-02-17 17:17:51 +00:00
|
|
|
.cornerRadius(4)
|
2023-01-17 18:41:46 +00:00
|
|
|
.onChange(of: scenePhase, perform: { scenePhase in
|
|
|
|
switch scenePhase {
|
|
|
|
case .background, .inactive:
|
|
|
|
viewModel.pause()
|
|
|
|
case .active:
|
2023-02-04 06:53:03 +00:00
|
|
|
if preferences.autoPlayVideo {
|
|
|
|
viewModel.play()
|
|
|
|
}
|
2023-01-17 18:41:46 +00:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2022-12-24 07:29:45 +00:00
|
|
|
}
|
|
|
|
}
|