使用MPMoviePlayerController播放视频

By | 2015年6月3日
var moviePlayerControl:MPMoviePlayerController!
override func viewDidLoad() {
    super.viewDidLoad()

    //视频路径
    let path = NSBundle.mainBundle().pathForResource("116241", ofType: "mp4")
    //初始化Control
    moviePlayerControl = MPMoviePlayerController(contentURL: NSURL.fileURLWithPath(path!))
    //设置View大小,全屏
    //mediaPlayerControl.view.frame = self.view.bounds
    //设置View大小,自定义大小
    moviePlayerControl.view.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.6)
    //整一个背景色
    moviePlayerControl.view.backgroundColor = UIColor.blueColor()
    //添加view
    self.view.addSubview(moviePlayerControl.view)

    //显示方式
    moviePlayerControl.controlStyle =  MPMovieControlStyle.Embedded
    //内容缩放方式,默认是AspectFit
    moviePlayerControl.scalingMode = MPMovieScalingMode.AspectFit
    //循环播放
    moviePlayerControl.repeatMode = MPMovieRepeatMode.One
    //视频文件类型,本地的或者是网络下载的
    moviePlayerControl.movieSourceType = MPMovieSourceType.File

    //开始播放
    moviePlayerControl.play()

    //暂停播放
    moviePlayerControl.pause()

    //结束播放
    moviePlayerControl.stop()
}