0

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.
    }

}
Chris Loonam
  • 5,735
  • 6
  • 41
  • 63
Susan Starkman
  • 163
  • 1
  • 1
  • 7
  • Any error messages? Bugs? – saagarjha Aug 21 '15 at 00:27
  • No error messages. Just won't play the video. I've tried several different video URL's and none will play. Controller shows up fine put when I press the play button the screen just goes black. – Susan Starkman Aug 21 '15 at 01:53
  • 2
    AVPlayer isn't going to play using a link to a YouTube page. You need to give it a link to an actual video file or HLS playlist. – ChrisH Aug 21 '15 at 18:54

1 Answers1

2

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")
Community
  • 1
  • 1
Jonathan
  • 291
  • 2
  • 6