I’m trying to create a video player in SwiftUi with a AVPlayerViewController. I found that I have to use a struct that conforms to the UIViewControllerRepresentable protocol
struct AVPlayerControllerRepresented : UIViewControllerRepresentable {
typealias UIViewControllerType = AVPlayerViewController
var player : AVPlayer
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = player
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
}
}
but Xcode keeps saying it doesn’t conform
I tried pressing the fix button next to the error but it just adds the same functions that are already there
func makeUIViewController(context: Context) -> AVPlayerViewController {
<#code#>
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
<#code#>
}
2
Answers
I solved it after a while, in some other file there was a
typealias
for the typeContext
after removing it everything worksJust make sure you’re importing
AVKit
and your code should work. (No need for thetypealias
)