我試圖在包含視頻和一些文本的子視圖內顯示視頻。我用recommended UIView subclass from Apple創建一個UIView包含我AVPlayer:AVPlayer/AVPlayerLayer不會出現在使用自動佈局的子視圖中
import Foundation
import AVFoundation
class JSAVPlayerView : UIView {
var player:AVPlayer? {
get {
return (self.layer as! AVPlayerLayer).player
}
set(newPlayer) {
(self.layer as! AVPlayerLayer).player = newPlayer
}
}
override class func layerClass() -> AnyClass {
return AVPlayerLayer.self
}
}
我在UICollectionViewCell
使用JSAVPlayerView
作爲一個子視圖,如下圖所示,但我只看到了JSAVPlayerView
的背景。
class JSOnBoardingTutorialPageView : UICollectionViewCell {
// MARK: member variables
private var tutorialVideoPlayer:AVPlayer!
private var videoPlayerView:JSAVPlayerView = JSAVPlayerView()
private var tutorialLabel:UILabel = UILabel()
var tutorialText:String = "" {
didSet {
tutorialLabel.text = tutorialText
}
}
// MARK: auto layout variables
private var metrics:[String:CGFloat] = [
"marginTop" : 30,
"marginLeft" : 30,
"marginRight" : 30,
"marginBottom" : 30,
"textHeight" : 100,
]
private var autolayoutConstraints:[NSLayoutConstraint] = [NSLayoutConstraint]()
// MARK: constructors
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.jsBrandBlack()
self.setupSubviews()
}
init() {
super.init(frame: CGRect.zero)
self.backgroundColor = UIColor.jsBrandBlack()
self.setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: subview setup
func setupSubviews() {
self.setupTutorialVideoView()
self.setupTutorialLabel()
self.setupConstraints()
self.addConstraints(self.autolayoutConstraints)
}
func setupTutorialVideoView() {
self.videoPlayerView.translatesAutoresizingMaskIntoConstraints = false
self.videoPlayerView.backgroundColor = UIColor.blueColor()
if let player = self.tutorialVideoPlayer {
self.videoPlayerView.player = player
}
self.addSubview(self.videoPlayerView)
}
func setupTutorialLabel() {
self.tutorialLabel.translatesAutoresizingMaskIntoConstraints = false
self.tutorialLabel.textColor = UIColor.jsLightGray()
self.tutorialLabel.font = UIFont.jsH3WithWeight(.Normal)
self.tutorialLabel.numberOfLines = 0
self.tutorialLabel.textAlignment = .Center
self.addSubview(self.tutorialLabel)
}
func setupConstraints() {
let views:[String:AnyObject] = [
"video" : self.videoPlayerView,
"text" : self.tutorialLabel
]
self.autolayoutConstraints.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-marginTop-[video]-marginBottom-[text([email protected])]-|",
options: .AlignAllCenterX,
metrics: self.metrics,
views: views
)
)
self.autolayoutConstraints.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-marginLeft-[video]-marginRight-|",
options: NSLayoutFormatOptions(),
metrics: self.metrics,
views: views
)
)
self.autolayoutConstraints.appendContentsOf(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-marginLeft-[text]-marginRight-|",
options: NSLayoutFormatOptions(),
metrics: self.metrics,
views: views
)
)
}
// MARK: video setup
func setVideo(url:NSURL) {
self.tutorialVideoPlayer = AVPlayer(URL: url)
self.videoPlayerView.player = self.tutorialVideoPlayer
let playerLayer:AVPlayerLayer = self.videoPlayerView.layer as! AVPlayerLayer
playerLayer.frame = self.videoPlayerView.frame
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
}
func playVideo() {
let playerLayer = self.videoPlayerView.layer as! AVPlayerLayer
if playerLayer.readyForDisplay {
self.tutorialVideoPlayer.play()
} else {
print("Player not ready to play")
}
}
func pauseVideo() {
self.tutorialVideoPlayer.pause()
}
// MARK: layout hack
override func layoutSubviews() {
super.layoutSubviews()
self.tutorialLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.tutorialLabel.bounds)
super.layoutSubviews()
}
}
在我的視圖控制器,我設置了UICollectionViewCell(和視頻我想顯示的URL)是這樣的:
let cell:JSOnBoardingTutorialPageView = collectionView.dequeueReusableCellWithReuseIdentifier(self.CollectionViewCellReuseIdentifer, forIndexPath: indexPath) as! JSOnBoardingTutorialPageView
if let index = JSOnBoardingTutorialPage(rawValue: indexPath.row) {
cell.setVideo((self.pageData[index]?.video)!)
print(String(format: "video url: %@", (self.pageData[index]?.video)!))
cell.tutorialText = (self.pageData[index]?.text)!
cell.playVideo()
}
return cell
如何讓我的視頻中顯示其貨櫃JSAVPlayerView
?
是否「播放器未準備好播放」? –
是的。每次。 – Matt
'playerLayer.error'和'playerLayer.status'告訴你什麼? –