Reputation: 276
I'm trying to initialise an MPMoviePlayerController
in a custom UIView
. I do hear the sound but no view. I think something is wrong with initialising the view. I tried it by implementing in the ViewController
, then it does work.
Here my code:
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
Upvotes: 0
Views: 109
Reputation: 1792
You need to set frame and add subview after create videoPlayer
object. In addDataToView
you init videoPlayer
again so every settings before that are cleared.
Upvotes: 4