我正在使用library called Gallery从照片中选择一个视频。每当选择视频时,我都希望在UIViewController
中的某个地方播放所选视频(静默)。
对于此问题,插件的相关部分是在选择视频时调用的函数func galleryController(_ controller: GalleryController, didSelectVideo video: Video)
。我最初尝试使用function which you'll find under this link (presented by the library)测试它是否可行但没有任何反应:没有控制器出现。在做了一些研究后,我相信我需要将临时文件(在tempPath
下找到)转移到文档目录 - 这似乎也不起作用。您将在下面找到我目前使用的代码。
如果我打印tempPath
(临时路径)和videoFileUrl
(文件应该转移到的URL),我会分别获得以下网址:
TEMPPATH: 可选的(文件:///private/var/mobile/Containers/Data/Application/D9B0DCB6-8F99-4284-9BFF-52A284C6F1AE/tmp/F3DC43D0-491A-4548-9960-D01686B54ACB.mp4)
videoFileUrl: 文件:///var/mobile/Containers/Data/Application/D9B0DCB6-8F99-4284-9BFF-52A284C6F1AE/Documents/F3DC43D0-491A-4548-9960-D01686B54ACB.mp4
internal var gallery: GalleryController?
internal let editor: VideoEditing = VideoEditor()
func galleryController(_ controller: GalleryController, didSelectVideo video: Video) {
controller.dismiss(animated: true, completion: nil)
editor.edit(video: video) { (editedVideo: Video?, tempPath: URL?) in
DispatchQueue.main.async {
guard let temporaryPath = tempPath else { return }
let fileName = temporaryPath.lastPathComponent
let fileManager = FileManager.default
let videoFileUrl = try! fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(fileName)
if fileManager.fileExists(atPath: videoFileUrl.path) == false {
try! fileManager.copyItem(at: temporaryPath, to: videoFileUrl)
}
let player = AVPlayer(url: videoFileUrl)
//print(tempPath)
//print(videoFileUrl)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
}
}
gallery = nil
}