我正在尝试在包含视频和一些文本的子视图中显示视频.我使用
recommended UIView subclass from Apple来创建一个UIView来包含我的AVPlayer:
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 } }
我使用JSAVPlayerView作为UICollectionViewCell中的子视图,如下所示,但我只看到了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--marginBottom-[text(text@750)]-|",options: .AlignAllCenterX,metrics: self.metrics,views: views ) ) self.autolayoutConstraints.appendContentsOf( NSLayoutConstraint.constraintsWithVisualFormat( "H:|-marginLeft--marginRight-|",options: NSLayoutFormatOptions(),views: views ) ) self.autolayoutConstraints.appendContentsOf( NSLayoutConstraint.constraintsWithVisualFormat( "H:|-marginLeft-[text]-marginRight-|",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中?
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。