iPhone - 使用Swift播放mp4视频的问题 - iOS

时间:2015-03-21 07:04:06

标签: ios swift nsurl

我使用此代码从资源中准备好mp4文件的路径并播放它。

我使用了这段代码:

func playVideo(videoName : String, type: String) {


    let url = urlForFile(videoName, fileType: type)
    if let url = url {
        /* use the scene */
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = self.view.bounds

            player.controlStyle = MPMovieControlStyle.None
            player.prepareToPlay()
            player.scalingMode = .AspectFill
            self.view.addSubview(player.view)
        }
        println("it started!")
    } else {
        /* the scene couldn't be intialized */
        finishedVideo()
    }

}

func urlForFile (fileName : String, fileType: String) -> NSURL? {
    let path = NSBundle.mainBundle().pathForResource(fileName, ofType:fileType)
    if let path = path {
        return NSURL(fileURLWithPath:path)
    } else {
        return nil
    }
}

我用这个函数调用实现它:

playVideo("splash_video", type: "mp4");

文件在这里:

Screenshot

urlForFile函数始终返回nil

我还尝试将视频转换为m4v和mov,仍然得到相同的结果" nil "

那么我怎样才能在SWIFT播放视频!?

1 个答案:

答案 0 :(得分:3)

您的代码看起来正确,没有问题。您可能忘了将该文件添加到目标会员资格中。

  1. 选择您的文件
  2. 选择文件检查器
  3. 在目标成员资格下选择目标
  4. Target