WIP
This commit is contained in:
@@ -7,80 +7,42 @@
|
||||
|
||||
import SwiftUI
|
||||
import AVKit
|
||||
import SafariServices
|
||||
import Combine
|
||||
|
||||
struct VideoPlayerView: View {
|
||||
@State var url: URL
|
||||
@Environment(\.dismiss) var dismiss
|
||||
@Binding var avPlayer: AVPlayer
|
||||
var showDoneButton = true
|
||||
|
||||
var pub = NotificationCenter.default.publisher(for: .AVPlayerItemDidPlayToEndTime)
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
Button(action: {
|
||||
dismiss()
|
||||
}, label: {
|
||||
Text("Done")
|
||||
})
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(Color(uiColor: UIColor(red: 0.11, green: 0.11, blue: 0.12, alpha: 1)))
|
||||
if self.showDoneButton {
|
||||
Button(action: {
|
||||
dismiss()
|
||||
}, label: {
|
||||
Text("Done")
|
||||
})
|
||||
.padding()
|
||||
.frame(maxWidth: .infinity)
|
||||
.background(Color(uiColor: UIColor(red: 0.11, green: 0.11, blue: 0.12, alpha: 1)))
|
||||
}
|
||||
|
||||
VideoViewControllerView(url: $url)
|
||||
VideoPlayer(player: avPlayer)
|
||||
.onAppear{
|
||||
avPlayer.play()
|
||||
}
|
||||
}
|
||||
.background(.black)
|
||||
}
|
||||
}
|
||||
|
||||
struct VideoViewControllerView: UIViewControllerRepresentable {
|
||||
@Binding var url: URL
|
||||
|
||||
func makeUIViewController(context: Context) -> VideoViewController {
|
||||
return VideoViewController(videoURL: url)
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: VideoViewController, context: Context) {
|
||||
if url != uiViewController.videoURL {
|
||||
uiViewController.videoURL = url
|
||||
uiViewController.layoutView()
|
||||
.onReceive(pub) { (output) in
|
||||
avPlayer.pause()
|
||||
avPlayer.seek(to: .zero)
|
||||
avPlayer.play()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VideoViewController: UIViewController {
|
||||
var videoURL: URL
|
||||
|
||||
init(videoURL: URL) {
|
||||
self.videoURL = videoURL
|
||||
super.init(nibName: nil, bundle: nil)
|
||||
layoutView()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
}
|
||||
|
||||
func layoutView() {
|
||||
self.view.subviews.forEach({
|
||||
$0.removeFromSuperview()
|
||||
})
|
||||
|
||||
let sfVC = SFSafariViewController(url: self.videoURL)
|
||||
sfVC.view.translatesAutoresizingMaskIntoConstraints = false
|
||||
view.backgroundColor = .green
|
||||
self.addChild(sfVC)
|
||||
sfVC.didMove(toParent: self)
|
||||
sfVC.view.backgroundColor = .magenta
|
||||
sfVC.view.frame = self.view.bounds;
|
||||
self.view.addSubview(sfVC.view)
|
||||
sfVC.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
|
||||
sfVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
||||
sfVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
|
||||
sfVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
|
||||
}
|
||||
}
|
||||
|
||||
//struct VideoPlayerView_Previews: PreviewProvider {
|
||||
// static let exercise = PreviewData.parseExercises().first!
|
||||
|
||||
Reference in New Issue
Block a user