Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift AvPlayer可以';无法从自定义路径播放视频_Swift_Avplayer - Fatal编程技术网

Swift AvPlayer可以';无法从自定义路径播放视频

Swift AvPlayer可以';无法从自定义路径播放视频,swift,avplayer,Swift,Avplayer,我正在尝试播放我在此路径中录制的视频。但是我不能从这个路径播放视频。我该怎么办 let pathMovie = (NSHomeDirectory() as NSString).stringByAppendingPathComponent("Documents/Movie.mp4") let fileUrl = NSURL(fileURLWithPath: pathMovie) self.videoFrame = view.frame self.f

我正在尝试播放我在此路径中录制的视频。但是我不能从这个路径播放视频。我该怎么办

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{Error Domain=nsossStatUserRorDomain Code=-12893“(null)”})


通过添加以下两种方法,可以从自定义路径播放视频:

注意:请将您的视频名称作为getCustomVideoPath方法的参数传递

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()
    }
}

你确定这个文件存在吗?是的,我使用设备屏幕查看了文件确保你的电影编解码器是H264/AAC Audio不要硬编码你的文件路径。您应该使用URLForDirectory获取应用程序文档目录URLForDirectory。首先,您使用的是iOS还是OSX?沙箱启用了吗?嘿@Murat Kaya你觉得我的答案有用吗?