我正在尝试使用MPMoviePlayerController
加载视频。我已将该文件添加到我的项目中,并添加到Build Phase" Copy Bundle Resource"。
当我调用该函数打开视频时,我可以看到它打印出来的路径,然后应用程序崩溃了' fileURLWithPath`。
我做错了什么? .mp4
苍蝇可以玩吗?
func firstView(){
if let filePath = NSBundle.mainBundle().pathForResource("firstVideo", ofType: "mp4") {
print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4
firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
}
firstMoviePlayer!.repeatMode = .None
firstMoviePlayer!.controlStyle = MPMovieControlStyle.Embedded
firstMoviePlayer!.view.frame = FirstTimeVideoView.frame
FirstTimeVideoView.addSubview(firstMoviePlayer!.view)
firstMoviePlayer!.play()
}
答案 0 :(得分:1)
你只需要改变:
if let filePath = NSBundle.mainBundle().pathForResource("firstVideo", ofType: "mp4") {
print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4
firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
}
TO:
if let filePath = NSBundle.mainBundle().URLForResource("firstVideo", ofType: "mp4") {
print(filePath)
//PRINTS: /var/mobile/Containers/Bundle/Application/B93BB049-DA87-4D89-AEBE-A5C92C01726E/MyApp.app/firstVideo.mp4
firstMoviePlayer!.contentURL = NSURL.fileURLWithPath(filePath) // fatal error
}