MPMoviePlayerController 5秒后停止播放 – Swift

我有一个video,我试图使用MPMoviePlayerController播放,它加载正常,但5秒后切出。 我发现这个post,但它并不适用于swift。

MPMoviePlayerController 5秒后停止播放video

这是我的代码。

import MediaPlayer class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var moviePlayer: MPMoviePlayerController? let url = NSURL(string: "http://path/to/video.m3u8") moviePlayer = MPMoviePlayerController(contentURL: url) if let player = moviePlayer { player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height) player.view.sizeToFit() player.scalingMode = MPMovieScalingMode.None player.movieSourceType = MPMovieSourceType.Streaming //player.repeatMode = MPMovieRepeatMode.One player.play() self.view.addSubview(player.view) NSNotificationCenter.defaultCenter().addObserver( self, selector: "metadataUpdated", name: MPMoviePlayerTimedMetadataUpdatedNotification, object: nil) } } 

你的moviePlayer能否超出范围? 你有没有试过把它变成一个成员variables?

moviePlayerviewDidLoad一个局部variables,所以一旦这个函数结束,我看不出有什么理由不解释你的播放器。

如果您将其作为课程的一个variables,则其生命周期将延长到与您class级的生命周期相匹配。

就像是

 class ViewController: UIViewController { var player: MPMoviePlayerController? override func viewDidLoad() { // ... self.player = MPMoviePlayerController(contentURL: url) // won't go out of scope at end of viewDidLoad() // ... }