1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Apple TV/PlayerView.swift

69 lines
1.8 KiB
Swift
Raw Normal View History

2021-06-11 04:20:10 +05:30
import AVKit
import Foundation
import SwiftUI
struct PlayerView: View {
2021-06-11 05:35:59 +05:30
@ObservedObject private var provider: VideoDetailsProvider
private var id: String
init(id: String) {
self.id = id
provider = VideoDetailsProvider(id)
}
2021-06-11 04:20:10 +05:30
var body: some View {
ZStack {
if let video = provider.video {
if video.url != nil {
PlayerViewController(video)
.edgesIgnoringSafeArea(.all)
}
if video.error {
Text("Video can not be loaded")
}
}
}
.task {
async {
provider.load()
}
}
}
}
struct PlayerViewController: UIViewControllerRepresentable {
var video: Video
init(_ video: Video) {
self.video = video
}
private var player: AVPlayer {
let item = AVPlayerItem(url: video.url!)
item.externalMetadata = [makeMetadataItem(.commonIdentifierTitle, value: video.title)]
return AVPlayer(playerItem: item)
}
private func makeMetadataItem(_ identifier: AVMetadataIdentifier, value: Any) -> AVMetadataItem {
let item = AVMutableMetadataItem()
item.identifier = identifier
item.value = value as? NSCopying & NSObjectProtocol
item.extendedLanguageTag = "und"
return item.copy() as! AVMetadataItem
}
func makeUIViewController(context _: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.modalPresentationStyle = .fullScreen
controller.player = player
controller.title = video.title
controller.player?.play()
return controller
}
func updateUIViewController(_: AVPlayerViewController, context _: Context) {}
}