这是我的代码,我希望视频可以填满整个屏幕(采用aspectFill)。
playerLayer.videoGravity = kCAGravityResizeAspectFill 应该可以实现Aspect Fill,但我仍然在屏幕的顶部和底部看到黑色边框。
最后一行本来应该循环播放视频,但它只会变暗并停止播放。
如果有任何建议,将不胜感激。
var moviePlayer: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Load the video from the app bundle.
let videoURL: NSURL = NSBundle.mainBundle().URLForResource("video", withExtension: "mp4")!
self.moviePlayer = AVPlayer(URL: videoURL)
self.moviePlayer.muted = true
let playerLayer = AVPlayerLayer(player: self.moviePlayer)
playerLayer.videoGravity = kCAGravityResizeAspectFill //this not working
playerLayer.zPosition = -1
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
self.moviePlayer.play()
// Loop video.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "loopVideo", name: AVPlayerItemDidPlayToEndTimeNotification, object: self.moviePlayer)
}
func loopVideo() {
self.moviePlayer.play()
}