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 memberresizeAspectFill
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)
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
You need to say:
avPlayerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill .rawValue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With