Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift 3 error: Type 'AVLayerVideoGravity' has no member 'resizeAspectFill'

I am working on an iOS app using Swift 3 where I have a video playing in the background. My code was working completely fine until randomly the error message:

Type AVLayerVideoGravity aka (NSString) has no member resizeAspectFill

appears. I can't figure out at all why resizeAspectFill is no longer being recognized. Is there something I'm missing? I've tried cleaning my project but that does not fix anything. Any help would be greatly appreciated :) Below is the code:

import Foundation
import AVFoundation
import UIKit

class HomeViewController: UIViewController {
// MARK: Properties
var avPlayer: AVPlayer!
var avPlayerLayer: AVPlayerLayer!
var paused: Bool = false

var enterButton: UIButton! = UIButton()

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Welcome"

    let theURL = Bundle.main.url(forResource:"hannah", withExtension: "mp4")

    avPlayer = AVPlayer(url: theURL!)
    avPlayerLayer = AVPlayerLayer(player: avPlayer)
    avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    avPlayer.volume = 0
    avPlayer.actionAtItemEnd = .none

    avPlayerLayer.frame = view.layer.bounds
    view.backgroundColor = .clear
    view.layer.insertSublayer(avPlayerLayer, at: 0)

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(playerItemDidReachEnd(notification:)),
                                           name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
                                           object: avPlayer.currentItem)
like image 840
Claudia Laurie Avatar asked Dec 14 '22 22:12

Claudia Laurie


2 Answers

From the documentation of AVPlayerLayer (unfortunately the link redirects to the Swift 4 version. To see the changes enable Show API changes at top right).

The video gravity determines how the video content is scaled or stretched within the player layer’s bounds. The player layer supports the following video gravity values:

  • AVLayerVideoGravityResizeAspect

  • AVLayerVideoGravityResizeAspectFill

  • AVLayerVideoGravityResize

The default value is AVLayerVideoGravityResizeAspect.


The enum case you are referring to belongs to Swift 4

like image 102
vadian Avatar answered May 07 '23 17:05

vadian


You need to say:

avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill .rawValue
like image 45
dijipiji Avatar answered May 07 '23 16:05

dijipiji