MPMoviePlayerController 在自定义 UIView 中不可见

MPMoviePlayerController not visible in custom UIView

我正在尝试在自定义 UIView 中初始化 MPMoviePlayerController。我确实听到了声音,但没有看到。我认为初始化视图有问题。我通过在 ViewController 中实施来尝试它,然后它确实有效。

这是我的代码:

import UIKit
import MediaPlayer

class CustomView: UIView {

var sponsor: Sponsors!

var videoContainer = UIView()
var videoPlayer = MPMoviePlayerController()

override init(frame: CGRect) {
    super.init(frame: frame)

    //Init
    layer.cornerRadius = 3.0
    backgroundColor = UIColor.whiteColor()

    //VideoContainer
    videoContainer.backgroundColor = UIColor.blackColor()

    //Adding the views
    addSubview(videoContainer)
    videoContainer.addSubview(videoPlayer.view)        
}

//MARK: InitCoder
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!
}

//MARK: AddDataToView
func addDataToView(sponsor: Sponsors) {

    //VideoPlayerx
    let path = NSBundle.mainBundle().pathForResource("movie5", ofType:"mov")
    let url = NSURL.fileURLWithPath(path!)
    videoPlayer = MPMoviePlayerController(contentURL: url)
    videoPlayer.prepareToPlay()
}

//MARK: LayoutSubViews
override func layoutSubviews() {
    super.layoutSubviews()        

    //VideoContainer
    videoContainer.frame = CGRectMake(8, 8, frame.size.width - 16, 219)

    //Video
    videoPlayer.view.frame = CGRectMake(8, 8, frame.size.width - 16, 219)
    videoPlayer.scalingMode = .AspectFill

您需要在 创建 videoPlayer 对象后设置框架并添加子视图 。在 addDataToView 中,您再次初始化 videoPlayer,以便清除之前的所有设置。