Swift 3.0
Necessary: import AVKit,import AVFoundation
If you want to use AVPlayerViewController:
let videoURL = NSURL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“)
let player = AVPlayer(url: videoURL! as URL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController,animated: true) {
playerViewController.player!.play()
}
or just AVPlayer:
let videoURL = NSURL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“)
let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
It’s better to put this code into the method: override func viewDidAppear(_ animated: Bool) or somewhere after.
Swift 2.x
AVPlayerViewController:
let videoURL = NSURL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“)
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController,animated: true) {
playerViewController.player!.play()
}
AVPlayer:
let videoURL = NSURL(string: “https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“)
let player = AVPlayer(URL: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
Objective-C
AVPlayerViewController:
NSURL *videoURL = [NSURL URLWithString:@”https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
or just AVPlayer:
NSURL *videoURL = [NSURL URLWithString:@”https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4“]; AVPlayer *player = [AVPlayer playerWithURL:videoURL]; AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player]; playerLayer.frame = self.view.bounds; [self.view.layer addSublayer:playerLayer]; [player play];