Play MP4 using MPMoviePlayerController() in Swift

℡╲_俬逩灬. 提交于 2019-12-06 16:31:59

问题


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.


回答1:


So this was blindly annoying:

All I did was change:

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

TO:

self.moviePlayerController.contentURL = NSURL.fileURLWithPath(filePath)


来源:https://stackoverflow.com/questions/24350482/play-mp4-using-mpmovieplayercontroller-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!