AVPlayerView.swift (760B)
1 // 2 // AVPlayerView.swift 3 // damus 4 // 5 // Created by Bryan Montz on 9/4/23. 6 // 7 8 import Foundation 9 import AVKit 10 import SwiftUI 11 12 struct AVPlayerView: UIViewControllerRepresentable { 13 14 let player: AVPlayer 15 16 func makeUIViewController(context: Context) -> AVPlayerViewController { 17 AVPlayerViewController() 18 } 19 20 func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) { 21 if uiViewController.player == nil { 22 uiViewController.player = player 23 player.play() 24 } 25 } 26 27 static func dismantleUIViewController(_ uiViewController: AVPlayerViewController, coordinator: ()) { 28 uiViewController.player?.pause() 29 uiViewController.player = nil 30 } 31 }