How to play video with AVPlayerViewController (AVKit) in Swift

前端之家收集整理的这篇文章主要介绍了How to play video with AVPlayerViewController (AVKit) in Swift前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://stackoverflow.com/questions/25932570/how-to-play-video-with-avplayerviewcontroller-avkit-in-swift

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];

原文链接:https://www.f2er.com/swift/322999.html

猜你在找的Swift相关文章