我有一個視頻,我試圖播放使用MPMoviePlayerController,它加載正常,但5秒後切出。我發現了這篇文章,但它並不適用於swift。MPMoviePlayerController停止播放5秒後 - Swift
MPMoviePlayerController stops playing the video after 5s
這裏是我的代碼。
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)
}
}
是您的視頻來直播或點播?如果不是,則使用MPMovieSourceType.File代替MPMovieSourceType.Streaming。 – deepax11 2015-06-16 12:49:22