带有swift 2.0 xcode 7 beta 3的AVPlayerViewController视频

时间:2015-07-22 02:34:03

标签: avplayer swift2 xcode7 ios9

我已经做了一些相当沉重的挖掘工作,我感觉并且无法在xcode 7中使用swift 2.0在应用中播放视频时找到错误的原因......

我可能会遗漏一些东西...我是新手在应用中添加视频但是我最近看到了很多关于这个主题的不同代码。感谢您的帮助或指示!

import UIKit
import AVKit
import AVFoundation


class ProfileTableViewController: UITableViewController {

    var moviePlayer : AVPlayerViewController!


    @IBAction func playButton(sender: AnyObject) {

        let path = NSBundle.mainBundle().pathForResource("SampleVideo_1080x720_1mb.mp4", ofType:"mp4")

        let url = NSURL.fileURLWithPath(path!)

        self.moviePlayer = AVPlayerViewController()

        if let player = self.moviePlayer {

            player.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
            player.view.sizeToFit()
            /* Scale the movie player to fit the aspect ratio */
            player.scalingMode = .AspectFit
            player.controlStyle = MPMovieControlStyle.None
            player.movieSourceType = MPMovieSourceType.File; player.repeatMode = MPMovieRepeatMode.One;
            player.play()
            self.view.addSubview(player.view)



        }


    } 

2 个答案:

答案 0 :(得分:2)

" SampleVideo_1080x720_1mb.mp4",ofType:" mp4"

不应该是"" SampleVideo_1080x720_1mb",ofType:" mp4"

否则寻找" SampleVideo_1080x720_1mb.mp4.mp4"

答案 1 :(得分:1)

我认为问题是pathForRecource结尾处的.mp4。 代替 :  let path = NSBundle.mainBundle().pathForResource("SampleVideo_1080x720_1mb.mp4", ofType:"mp4"), 它应该是这样的:let path = NSBundle.mainBundle().pathForResource("SampleVideo_1080x720_1mb", ofType:"mp4")