Play MP4 using MPMoviePlayerController() in Swift

2019-04-17 08:35发布

I can't for the life of me figure out a way to play an MP4 that takes up the entire background in a UIViewController.

So far I have the following, which doesn't even play the video at all.

I can confirm that the bokeh.mp4 video exists because if I change the file to something else then it throws an error that it's missing.

override func viewDidAppear(animated: Bool)  {
    let filePath = NSBundle.mainBundle().pathForResource("bokeh", ofType: "mp4")
    self.moviePlayerController.contentURL = NSURL(string: filePath)
    self.moviePlayerController.prepareToPlay()
    self.moviePlayerController.repeatMode = .One
    self.moviePlayerController.controlStyle = .Embedded
    self.view.addSubview(self.moviePlayerController.view)
}

I get an error in the console:

2014-06-22 21:22:42.347 MoviePlayer[26655:60b] _itemFailedToPlayToEnd: {
    kind = 1;
    new = 2;
    old = 0;
}

I've also tried adding a UIView that takes up the entire screen and adding the player to that view, but it's the same problem. It doesn't start playing.

I'm trying to achieve the same effect that Vine has when you first load up the application where it has the video playing in the background.

enter image description here

1条回答
虎瘦雄心在
2楼-- · 2019-04-17 08:46

So this was blindly annoying:

All I did was change:

self.moviePlayerController.contentURL = NSURL(string: filePath)

TO:

self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)
查看更多
登录 后发表回答