我有一个本地视频播放使用AVPlayer和AVPlayerLayer(斯威夫特的iOS 8.2),我需要调整AVPlayerLayer(introPlayerLayer当它的父视图() introView)被调整,包括对取向变化.到目前为止,我所尝试过的任何东西似乎都没有用,而且playerLayer它的大小和位置都不对.
该AVPlayer设置使用本地MP4视频,并且AVPlayerLayer正在使用该播放器初始化.然后将该图层作为子图层添加到父视图中.
这是我的代码:
func playIntro() {
let path = NSBundle.mainBundle().pathForResource("intro", ofType: "mp4")
let url = NSURL.fileURLWithPath(path!)
let introPlayer = AVPlayer(URL: url)
introPlayer.allowsExternalPlayback = false
var introPlayerLayer = AVPlayerLayer(player: introPlayer)
introView.layer.addSublayer(introPlayerLayer)
introPlayerLayer.frame = introView.bounds
NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: introPlayer.currentItem)
introView.hidden = false
introPlayer.play()
}
Run Code Online (Sandbox Code Playgroud)
我应该添加AVPlayerLayer作为子层UIView,或者我应该继承父容器(UIView),如https://developer.apple.com/library/ios/documentation/AudioVideo/的"播放器视图"部分所示Conceptual/AVFoundationPG/Articles/02_Playback.html.如果是这样,我将如何PlayerView在Swift中编写该子类(在Objective-C中显示)?提前感谢您的任何帮助.