gpt4 book ai didi

ios - 视频播放器崩溃 pathForResource Swift iOS

转载 作者:行者123 更新时间:2023-11-28 10:01:22 25 4
gpt4 key购买 nike

目前我尝试设置一个简单播放视频的小应用程序(只为我的女儿),但它总是在“路径”上崩溃

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

崩溃发生在这里 let path = NSBundle.mainBundle().pathForResource("video", ofType:"mp4")

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

最佳答案

您必须将 Build Phases 下 Xcode 目标中的视频添加到 Copy Bundle Resources

关于ios - 视频播放器崩溃 pathForResource Swift iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28146120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com