I want to download audio file, save it in photos library and play after download. But this code not saving video in library.
Is it possible to download directly in photos library or any other folder externally.
My code is from here Swift - Download a video from distant URL and save it in an photo album
DispatchQueue.global(qos: .background).async {
if let url = URL(string: self.audioURL!),
let urlData = NSData(contentsOf: url) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath="\(documentsPath)/tempFile.mp3"
print(filePath)
DispatchQueue.main.async {
urlData.write(toFile: filePath, atomically: true)
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))
}) { completed, error in
if completed {
print("Video is saved!")
}
}
}
}
}