2015-11-11 41 views
4

我試圖在包含視頻和一些文本的子視圖內顯示視頻。我用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

+0

是否「播放器未準備好播放」? –

+0

是的。每次。 – Matt

+0

'playerLayer.error'和'playerLayer.status'告訴你什麼? –

回答

1

我認爲問題是你永遠不會調用AVPlayer的構造函數(據我所見,你提供的代碼)。有一個功能func setVideo(url: NSURL),但你不要調用這個函數。

+0

爲什麼我需要這個?我的視頻不包含音頻。 – Matt

+0

對不起,沒有'音頻'。習慣的力量。 ;-) –

+0

我在'setVideo(url:NSURL)'中做。我在另一個文件中設置了URL,這裏沒有顯示。這個視圖包含在'UICollectionView'中(這就是爲什麼它擴展了'UICollectionViewCell')。 – Matt

相關問題