我正在尝试使用 AVPlayer 播放视频,但在提供 pathForResource 时出错。尝试了所有可能的包装可选值的方法,但没有成功。任何人都可以帮助我。
let userdefault = NSUserDefaults.standardUserDefaults()
let mediaPath = userdefault.URLForKey("VideoPath")
print(mediaPath)
let movieString = mediaPath!.path
let pathExtension = mediaPath!.pathExtension
let newPath = movieString!.stringByReplacingOccurrencesOfString(".\(pathExtension)", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(newPath)
let path = NSBundle.mainBundle().pathForResource(newPath, ofType: ".\(pathExtension)")
print(path)
if let pathString = path {
let url = NSURL(fileURLWithPath: pathString)
myplayer = AVPlayer(URL: url)
myplayer.play()
}
else {
print("Oooopppsss, something wrong when playing video")
}