iosswiftavplayeravplayerviewcontroller

Removing the QuickTime icon from AVPlayerViewController


I am building a screen where users can play audio files using an AVPlayerViewController. The problem is that I can't get rid of the QuickTime logo in the player view, see screenshot:

enter image description here

My code:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
    guard let url = URL(string: filePath!) else {
        return
    }

    let player = AVPlayer(url: url)
    let controller = AVPlayerViewController()
    controller.modalPresentationStyle = .overFullScreen
    controller.player = player

    self.present(controller, animated: true) {
        player.play()
    }
} catch {
}

I've tried adding an UIImageView using controller.contentOverlayView?.addSubView(), but I can't center that properly. How do I customize the layout of the player without having to build my own interface from scratch?


Solution

  • What you tried is correct: just add a subview to the content overlay view. But you left out one step: you must give both the subview and the content overlay view constraints, to make them cover the player controller’s view completely.

    Example (my av is your controller):

                let iv = UIView()
                iv.backgroundColor = .white
                av.contentOverlayView!.addSubview(iv)
                let v = av.contentOverlayView!
                iv.translatesAutoresizingMaskIntoConstraints = false
                NSLayoutConstraint.activate([
                    iv.bottomAnchor.constraint(equalTo:v.bottomAnchor),
                    iv.topAnchor.constraint(equalTo:v.topAnchor),
                    iv.leadingAnchor.constraint(equalTo:v.leadingAnchor),
                    iv.trailingAnchor.constraint(equalTo:v.trailingAnchor),
                ])
                NSLayoutConstraint.activate([
                    v.bottomAnchor.constraint(equalTo:av.view.bottomAnchor),
                    v.topAnchor.constraint(equalTo:av.view.topAnchor),
                    v.leadingAnchor.constraint(equalTo:av.view.leadingAnchor),
                    v.trailingAnchor.constraint(equalTo:av.view.trailingAnchor),
                ])