我正在尝试播放由此录制在此路径中的视频。但我不能从这条路上播放视频。我该怎么办?
let pathMovie = (NSHomeDirectory() as NSString).stringByAppendingPathComponent("Documents/Movie.mp4")
let fileUrl = NSURL(fileURLWithPath: pathMovie)
self.videoFrame = view.frame
self.fillMode = .ResizeAspectFill
self.alwaysRepeat = true
self.sound = true
self.startTime = 0.0
self.alpha = 1
self.backgroundColor = UIColor.blackColor()
self.contentURL = fileUrl
这是我得到的错误。
失败:可选(错误域= AVFoundationErrorDomain代码= -11800"操作无法完成" UserInfo = {NSLocalizedFailureReason =发生未知错误(-12893),NSLocalizedDescription =操作无法完成,NSURL = file:///var/mobile/Containers/Data/Application/.....-...-...-....-..../Documents/Movie.mp4,NSUnderlyingError = 0x150a38e80 {错误域= NSOSStatusErrorDomain代码= -12893"(null)"}})
答案 0 :(得分:1)
You can play video from custom path by adding following two methods :
Note : Please pass your video name as argument for getCustomVideoPath method.
Swift 4
func getCustomVideoPath(name: String) -> String {
let strDocumentDirectory = self.getDocumentDirectoryPath()
return "\(strDocumentDirectory)/\(name).mp4"
}
func getDocumentDirectoryPath() -> String {
let arrPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
return arrPaths[0]
}
//----------------------------------------------------------------
func playCustomVideo() {
let strVideoPath = self.getCustomVideoPath(name:"Movie")
let urlVideoPath = URL(fileURLWithPath: strVideoPath)
let newPlayerItem = AVPlayerItem(url: urlVideoPath)
self.avPlayer = AVPlayer(playerItem: newPlayerItem)
avPlayerController.player = self.avPlayer
self.avPlayer?.addObserver(self, forKeyPath: "status", options: NSKeyValueObservingOptions(rawValue: 0), context: MPPlayableContentManagerContext.observationInfo())
self.present(avPlayerController, animated: true) {
self.avPlayer?.play()
}
}