Last active
June 9, 2023 00:29
-
-
Save zwaldowski/8cc827f0c59a1d71e5a90a7241bd8800 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AVPlayerViewWrapper: UIViewControllerRepresentable { | |
var url: URL | |
var shouldStartPlayback: Bool | |
func makeUIViewController(context: Context) -> AVPlayerViewController { | |
let controller = AVPlayerViewController() | |
controller.player = AVPlayer(url: url) | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) { | |
if shouldStartPlayback { | |
uiViewController.player?.play() | |
} | |
} | |
} | |
struct VideoPlayerView: View { | |
var url: URL | |
@State var shouldStartPlayback = false | |
var body: some View { | |
VStack { | |
AVPlayerViewWrapper(url: url, shouldStartPlayback: shouldStartPlayback) | |
.aspectRatio(contentMode: .fit) | |
.padding(.vertical, 0) | |
.onAppear { | |
shouldStartPlayback = true | |
} | |
Spacer() | |
} | |
} | |
} | |
struct VideoPlayerView_Previews: PreviewProvider { | |
static var previews: some View { | |
VideoPlayerView(url: URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_adv_example_hevc/master.m3u8")!) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct AVPlayerViewWrapper: UIViewControllerRepresentable { | |
var url: URL | |
@Binding var isPlaying: Bool | |
func makeUIViewController(context: Context) -> AVPlayerViewController { | |
let player = AVPlayer(url: url) | |
NotificationCenter.default.addObserver(context.coordinator, selector: #selector(Coordinator.noteRateDidChange), name: AVPlayer.rateDidChangeNotification, object: player) | |
let controller = AVPlayerViewController() | |
controller.player = player | |
return controller | |
} | |
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) { | |
if isPlaying { | |
uiViewController.player?.play() | |
} else { | |
uiViewController.player?.pause() | |
} | |
} | |
func makeCoordinator() -> Coordinator { | |
Coordinator() | |
} | |
class Coordinator { | |
var isPlaying: Binding<Bool>? | |
@objc func noteRateDidChange(_ note: Notification) { | |
guard let player = note.object as? AVPlayer else { return } | |
isPlaying?.wrappedValue = player.rate != 0 | |
} | |
} | |
} | |
struct VideoPlayerView: View { | |
var url: URL | |
@State var isPlaying = false | |
var body: some View { | |
VStack { | |
AVPlayerViewWrapper(url: url, isPlaying: $isPlaying) | |
.aspectRatio(contentMode: .fit) | |
.padding(.vertical, 0) | |
.onAppear { | |
isPlaying = true | |
} | |
Spacer() | |
} | |
} | |
} | |
struct VideoPlayerView_Previews: PreviewProvider { | |
static var previews: some View { | |
VideoPlayerView(url: URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_adv_example_hevc/master.m3u8")!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Additional exercise for the reader: it would likely be prudent for
updateUIViewController
to check if theurl
has changed, as the system can and will reuse representable views with different parameters.