无法在iOS中的documentsDirectory中播放视频

时间:2017-01-10 10:27:47

标签: ios iphone swift3 avfoundation avkit

我正在尝试播放下载到设备文档目录的视频。出于某种原因,它不会发生。我得到的就是这个符号:

enter image description here

这是我的代码:

let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let videoFileUrl = URL(fileUrlWithPath: documentPath)
let videoFilePath = videoFileUrl.appendingPathComponent("somVideo.mp4").path
if FileManager.default.fileExists(atPath: videFilePath){
    let player = AVPlayer(url: URL(string: videoFilePath)!)
    let playerViewController = AVPlayerViewController()
    playerViewController.player = player
    self.present(playerViewController, animated: true) {
        playerViewController.player!.play()
    }
}
else {
    print("File doesn't exist")
}

该文件存在于该位置 - 我已经检查了我的文档目录中的模拟器 - 并且未触发else语句。没有其他东西打印到控制台。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下是OP编辑到问题中的答案(随后回滚,因为答案应该是答案)。如果OP发布单独的答案,我将删除此答案。

我设法让我的同事得到了一些帮助。如果有人感兴趣的话,这就是答案:

let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
if let docDir : URL = urls.first {
    let videoUrl = docDir.appendingPathComponent("\(objectId).mp4")
    do {
        let videoExists : Bool = try videoUrl.checkResourceIsReachable()
        if videoExists {
            let player = AVPlayer(url: videoUrl)
            let playerController = AVPlayerViewController()
            playerController.player = player
            self.present(playerController, animated: true) {
                playerController.player!.play()
            }
        }
        else {
            print("Video doesn't exist")
        }
    }
    catch let error as NSError {
        print(error)
    }
}