简体   繁体   中英

AvPlayer not playing video in Xcode 7

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

}

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM