MPMoviePlayerController没有停留在UIView的范围内

时间:2015-04-01 16:32:19

标签: ios swift mpmovieplayercontroller

我正在尝试将视频添加到模态窗口中的UIView容器中。当我添加视频时,它不会保持在UIView的界限范围内。以下是目前的情况:

Current State

您可以看到右侧边缘的灰色容器是容器,这里是添加视频的视图控制器。

import Foundation
import MediaPlayer

class EvalInstructionsVC: UIViewController {
    private var moviePlayer : MPMoviePlayerController?

    @IBOutlet weak var video: UIView!

    func playVideo() {
        let path = NSBundle.mainBundle().pathForResource("Time to Rotate Demo", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = video.bounds
            player.view.center = CGPointMake(CGRectGetMidX(video.bounds), CGRectGetMidY(video.bounds))
            player.prepareToPlay()
            player.scalingMode = MPMovieScalingMode.AspectFill
            video.addSubview(player.view)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        playVideo()
    }

    @IBAction func done(sender: UIBarButtonItem) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

正如您所看到的那样,边界正在被设置,并且它被添加为视频UIView的子视图,这是上面屏幕截图中的灰色框。

我很感激任何人都可以提供帮助。

干杯, 格里

1 个答案:

答案 0 :(得分:2)

稍后在视图控制器生命周期中调用player.view.frame = video.bounds

最简单的解决方案是从playVideo()致电viewDidAppear

override func viewDidAppear(animated: Bool) {
    playVideo()
}

仅供参考,我们的代码非常相似(基于相同的示例?),我认为您可能也不需要设置player.view.center