视频播放器崩溃pathForResource迅速iOSVideo Player Crashes pathForResource Swift iOS

- 此内容更新于:2015-01-27
主题:

现在我试图设置一个小应用程序,只是扮演一个视频(只是为了我的女儿),但它总是崩溃“路径” 这里发生了车祸让路径= NSBundle.mainBundle()。减低:pathForResource(“视频”、“mp4”) 致命错误:意外发现零而展开一个可选值 导致路径是空的

原文:

Currently i try to setup a small app that simply play a video (just for my daughter) but it always crashes on the "path"

import UIKit
import MediaPlayer

var moviePlayer : MPMoviePlayerController?

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        playVideo()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func playVideo() {

        let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = self.view.bounds
            player.prepareToPlay()
            player.scalingMode = .AspectFill
            self.view.addSubview(player.view)
        }
    }

}

enter image description here

The crash happens here let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")

fatal error: unexpectedly found nil while unwrapping an Optional value cause path is empty

IAmDav的回复:你添加你的文件(video.mp4)“包Resources"复印件;?

(原文:Are you added your file(video.mp4) to "Copy Bundle Resources"?)

解决方案:
你必须添加视频Xcode目标下副本包资源的构建阶段。
原文:

You have to add the video in your Xcode target under Build Phases to the Copy Bundle Resources.