Yokhen Yokhen - 7 months ago 23
Swift Question

Use of unresolved identifier 'AVPlayer'

I found the following code on the internet and copy and pasted it to test it, however it doesn't seem to be working.

import Foundation
import UIKit
import AVKit


class Video1: UIViewController {

var playerViewController = AVPlayerViewController()
var playView = AVPlayer() //Unresolved identifier 'AVPlayer'

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(animated: Bool) {
var fileURL = NSURL(fileURLWithPath: "/Users/User/Desktop/video.mp4")
playerView = AVPlayer(URL: fileURL) //Unresolved identifier 'playerView'
//Unresolved identifier 'AVPlayer'

playerViewController.player = playerView //Unresolved identifier 'playerView'

self.presentViewController(playerViewController, animated: true){
self.playerViewController.player?.play()
}


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}


I've imported the respective frameworks as shown, so it should work, but as seen, it fails.

enter image description here

Having little experience programming at iOS, I find myself at a loss. I would appreciate it if someone gave me a solution to this or at least point me in the right direction.

I will be happy to provide additional information if requested.

Answer

You need to import AVFoundation to access AVPlayer. AVKit is used for Mac development.

import AVFoundation
Comments