skip to Main Content

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


  1. Chosen as BEST ANSWER

    I solved it after a while, in some other file there was a typealias for the type Context after removing it everything works


  2. Just make sure you’re importing AVKit and your code should work. (No need for the typealias)

    import AVKit
    
    struct AVPlayerControllerRepresented: UIViewControllerRepresentable {
        var player: AVPlayer
    
        func makeUIViewController(context: Context) -> AVPlayerViewController {
            let controller = AVPlayerViewController()
            controller.player = player
            return controller
        }
    
        func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
        
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search