Swift AVPlayerViewController Add AirPlay

I am trying to enable AirPlay with mine AVPlayerViewController. In the document:

https://developer.apple.com/reference/avkit/avplayerviewcontroller

It indicates

AVPlayerViewController automatically supports AirPlay, but you need to do some project configuration and audio session before it can be enabled in your application.

On the tab, CapabilitiesI turned on the background for audio, AirPlay and Picture in Picture. I created AVPlayerViewControlleras follows:

// Create the view controller and player
let moviePlayerViewController: AVPlayerViewController = AVPlayerViewController()
let moviePlayer = AVPlayer(url: videoUrl!)

moviePlayer.allowsExternalPlayback = true
moviePlayer.usesExternalPlaybackWhileExternalScreenIsActive = true

// Initialize the AVPlayer
moviePlayerViewController.player = moviePlayer

// Present movie player and play when completion
self.present(moviePlayerViewController, animated: false, completion: {
    moviePlayerViewController.player?.play()
})

I thought two lines

moviePlayer.allowsExternalPlayback = true
moviePlayer.usesExternalPlaybackWhileExternalScreenIsActive = true

Add AirPlay support, but I'm wrong. I read that AirPlay can be used by adding MPVolumeView, but for a custom video controller, not the built-in one. Any help would be greatly appreciated.

+6
2

MPVolumeView. :

    let volumeView = MPVolumeView()
    self.view.addSubview(volumeView)

, , :

    let volumeView = MPVolumeView(frame: CGRect(x: -100, y: 0, width: 0, height: 0))
    self.addSubview(volumeView)
    for view: UIView in volumeView.subviews {
        if let button = view as? UIButton {
            button.sendActions(for: .touchUpInside)
            volumeView.removeFromSuperview()
            break
        }
    }

, , .

0
func appleTv()
    {
        let rect = CGRect(x: -100, y: 0, width: 0, height: 0)
        let airplayVolume = MPVolumeView(frame: rect)
        airplayVolume.showsVolumeSlider = false
        self.view.addSubview(airplayVolume)
        for view: UIView in airplayVolume.subviews {
            if let button = view as? UIButton {
                button.sendActions(for: .touchUpInside)
                break
            }
        }
        airplayVolume.removeFromSuperview()
    }
0

Source: https://habr.com/ru/post/1014779/


All Articles