我有同样的麻烦。这里是我的解决方案:
func playVideo() {
let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
if let player = moviePlayer {
player.view.frame = self.view.bounds
player.controlStyle = .None
player.prepareToPlay()
player.scalingMode = .AspectFit
self.view.addSubview(player.view)
}
}
不要忘记import MediaPlayer
并声明你的变量 - var moviePlayer: MPMoviePlayerController?
然后打电话给你的功能,当你要玩它playVideo()
看我为如何消除它的问题 - In iOS how do I programmatically add a video view and then remove it after it finishes playing using Swift? 我在那里得到了一个非常有用的答案。
另请注意,我禁用了我的控制器,因为我不希望它被看到。