iPhone - 使用 Swift 播放 mp4 视频时出现问题 - iOS

iPhone - issue in playing mp4 video with Swift - iOS

我使用这段代码从资源中准备好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");

文件在这里:

urlForFile函数总是returnsnil

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

所以我到底怎么才能在 SWIFT 中播放视频!?

您的代码看起来是正确的,没有任何问题。可能您忘记将该文件添加到您的目标成员中。

  1. Select 你的文件
  2. 选择文件检查器
  3. 低于目标会员资格 select 您的目标