Swift: resizing AVPlayerLayer to fit the boundaries of its parent container

I have local video playback using AVPlayerand AVPlayerLayer(Swift, iOS 8.2), and I need to resize AVPlayerLayer( introPlayerLayer) when its parent view ( introView) is resized, including changing orientation. So far, nothing I tried seems to work, but playerLayerhas the wrong size and position.

AVPlayerconfigured using local mp4 video, and AVPlayerLayerinitialized using this player. The layer is then added as a sublevel to the parent view.

Here is my code:

func playIntro() {
    let path = NSBundle.mainBundle().pathForResource("intro", ofType: "mp4")
    let url  = NSURL.fileURLWithPath(path!)
    let introPlayer           = AVPlayer(URL: url)
    introPlayer.allowsExternalPlayback = false

    var introPlayerLayer = AVPlayerLayer(player: introPlayer)
    introView.layer.addSublayer(introPlayerLayer)
    introPlayerLayer.frame = introView.bounds

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: introPlayer.currentItem)

    introView.hidden = false
    introPlayer.play()
}

AVPlayerLayer UIView, (UIView), " " https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html. , PlayerView ( Objective-C) Swift? .

+4
3

, , , . . :

override func viewWillLayoutSubviews() {
   previewScene()
}

func previewScene() {
    var firstAsset: AVAsset!, secondAsset: AVAsset!, thirdAsset: AVAsset!


     firstAsset = MediaController.sharedMediaController.s3Shot1
     secondAsset = MediaController.sharedMediaController.s3Shot2
     thirdAsset  = MediaController.sharedMediaController.s3Shot3


    if firstAsset != nil && secondAsset != nil && thirdAsset != nil {
        let assets = [firstAsset, secondAsset, thirdAsset]
        var shots = [AVPlayerItem]()

        for item in assets {
            let shot = AVPlayerItem(asset: item)
            shots.append(shot)
        }

        self.videoPlayer = AVQueuePlayer(items: shots)
        self.playerLayer = AVPlayerLayer(player: self.videoPlayer)
        self.playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
         AVPlayerItemDidPlayToEndTimeNotification, object: self.videoPlayer.items().last)
        self.videoPreviewLayer.layer.addSublayer(playerLayer)
        self.playerLayer.frame = self.videoPreviewLayer.bounds

    }

}
+3

Swift 3.

private func playVideo() {
        guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
            debugPrint("video.m4v not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: path))
        var playerLayer: AVPlayerLayer?
        playerLayer = AVPlayerLayer(player: player)
        playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer!.frame = self.view.frame
        self.view!.layer.addSublayer(playerLayer!)
        player.play()

    }
+3
introView.layer.masksToBounds = true

0

All Articles