我尝试使用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")
}