我有一个使用AVPlayer
and (Swift, iOS 8.2) 播放的本地视频,当它的父视图 ( ) 调整大小时AVPlayerLayer
,我需要调整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()
}
我应该将其添加AVPlayerLayer
为 的子层UIView
,还是应该将父容器 ( ) 子类化,如https://developer.apple.com/library/ios/documentation/AudioVideo/UIView
的“播放器视图”部分所示概念/AVFoundationPG/Articles/02_Playback.html。如果是这样,我将如何PlayerView
在 Swift 中编写该子类(在 Objective-C 中显示)?提前感谢您的任何帮助。