Reputation: 163
I've been trying to get this to work but can't seem to find the solution. I want a video to start playing on viewDidLoad. I can get the AVPlayerViewController to show up but no video plays. Any suggestions would be greatly appreciated.
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
var videoPlayer = AVPlayerViewController()
let videoPath = NSURL(string: "https://youtu.be/dWYNNQdD6rY")
override func viewDidLoad() {
super.viewDidLoad()
let player = AVPlayer(URL: videoPath!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 0
Views: 1140
Reputation: 301
I believe you need to feed the AVPlayer a URL to either an m3u8 file or directly to a movie. Some example m3u8 files can be found on Stack Overflow and other websites. Change your video path to be something like this:
let videoPath = NSURL(string: "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
Upvotes: 2