i need to download a mp3 file, but i only need the first 20 seconds of the song (or the entire song if the song is less than 20 sec).
this is how i download the entire song:
func downloadSong(audioUrl: URL) {
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in
guard let location = location, error == nil else { return }
do {
try FileManager.default.moveItem(at: location, to: destinationUrl)
// song available in destinationUrl
} catch let error as NSError {
}
}).resume()
}
is there a way to stop the download after 20 seconds? i know i can download the entire song and then cut it, but i want to be more efficient, especially if the song is very long.