Created
September 1, 2017 13:29
-
-
Save AlexZverusha/6e2eb6f9bef41aacca1a15093d58c603 to your computer and use it in GitHub Desktop.
AVPlayerLayer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
// 1 | |
let playerLayer = AVPlayerLayer() | |
playerLayer.frame = someView.bounds | |
// 2 | |
let url = NSBundle.mainBundle().URLForResource("someVideo", withExtension: "m4v") | |
let player = AVPlayer(URL: url) | |
// 3 | |
player.actionAtItemEnd = .None | |
playerLayer.player = player | |
someView.layer.addSublayer(playerLayer) | |
// 4 | |
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidReachEndNotificationHandler:", name: "AVPlayerItemDidPlayToEndTimeNotification", object: player.currentItem) | |
} | |
deinit { | |
NSNotificationCenter.defaultCenter().removeObserver(self) | |
} | |
// 5 | |
@IBAction func playButtonTapped(sender: UIButton) { | |
if playButton.titleLabel?.text == "Play" { | |
player.play() | |
playButton.setTitle("Pause", forState: .Normal) | |
} else { | |
player.pause() | |
playButton.setTitle("Play", forState: .Normal) | |
} | |
updatePlayButtonTitle() | |
updateRateSegmentedControl() | |
} | |
// 6 | |
func playerDidReachEndNotificationHandler(notification: NSNotification) { | |
let playerItem = notification.object as AVPlayerItem | |
playerItem.seekToTime(kCMTimeZero) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment