Skip to content

Instantly share code, notes, and snippets.

@sonsongithub
Created July 14, 2015 09:06
Show Gist options
  • Save sonsongithub/ec1962ecd1530e3c2869 to your computer and use it in GitHub Desktop.
Save sonsongithub/ec1962ecd1530e3c2869 to your computer and use it in GitHub Desktop.
Simple movie view using AVKit
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let item = AVPlayerItem(URL: NSURL(string: "http://i.imgur.com/4WHLcYw.mp4")!)
let con:AVPlayerViewController = AVPlayerViewController()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didPlayToEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)
let player = AVPlayer(playerItem: item)
player.actionAtItemEnd = .None
con.player = player
con.view.frame = CGRect(x: 0, y: 0, width: 320, height: 240)
self.view.addSubview(con.view)
con.player?.play()
con.showsPlaybackControls = false
}
func didPlayToEnd(notification:NSNotification) {
if let item = notification.object as? AVPlayerItem {
item.seekToTime(kCMTimeZero)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment