这是我所拥有的,我希望这个视频填满整个屏幕(aspectFill)
playerLayer.videoGravity = kCAGravityResizeAspectFill 应该进行纵横比填充,但我仍然在屏幕的顶部和底部看到黑条。
最后一行应该循环播放视频,但它只是变暗并停止播放。
任何建议表示赞赏。
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()
}