MPMoviePlayerController在自定义UIView中不可见

时间:2015-11-07 16:24:21

标签: ios swift uiview mpmovieplayercontroller

我尝试在自定义MPMoviePlayerController中初始化UIView。我确实听到了声音,但没有看到。我认为初始化视图有问题。我通过在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

1 个答案:

答案 0 :(得分:4)

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