Oliver
Oliver

Reputation: 119

AVPlayer not working iOS Swift

I'm trying to play an external html video with AVPlayer but for some reason I can't get it to work. I've been trying to read every question about this but none of the suggested answers seems to work. I've also tried to find if something has been changed for Swift 3 or Xcode 8.0 but with no luck.

Is my code wrong or do I need to do something else to make it work? Enable it in capabilities or something similar?

    let url = NSURL(fileURLWithPath: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
    let player = AVPlayer(url: url as URL)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player

    playerViewController.view.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
    self.view.addSubview(playerViewController.view)
    self.addChildViewController(playerViewController)

    player.play()

Upvotes: 1

Views: 4324

Answers (1)

Santosh
Santosh

Reputation: 2914

Try: let url = NSURL(string:"YOUR_URL")

string: produces an NSURL that represents the string as given. So the string like "http://www.google.com" and the URL represents http://www.google.com.

fileURLWithPath: takes a path, not a URL, and produces an NSURL that represents the path using a file:// URL. So if you give it /foo/bar/baz the URL would represent file:///foo/bar/baz.

Upvotes: 4

Related Questions