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.
Although Apple doesn’t provide any built-in application to directly download MP3 files from the web but you can use a third-party tool to mp3 music downloads for iPhone. Apple has a strict policy for illegal downloading of music to iPhone but there are many credible and widely used platforms to download MP3 and AAC files to your Apple device.
If you have mp3 files and want to transfer them to iOS device without using iTunes, Tenorshare iCareFone is the software you are looking for. I will elaborate how you can use it as an alternative to iTunes in the below paragraphs. Tenorshare iCareFone is the best software you can use to backup, manage, transfer and optimize your iPhone.
Google Music Play is a premium service by Google to download mp3 music to iPhone. It works on both Android and iOS devices. You have to sign in to your Google account to use this application. Once you are logged out of your account, all your downloaded music is deleted from the device.
Tenorshare iCareFone is the best software you can use to backup, manage, transfer and optimize your iPhone. It helps you solve all the problems of your iOS device with just one click. You can use it to transfer all files from your computer to iPhone.
MP3 file is divided into a small blocks - frames. Each frame has constant time length 0.026 sec. But size of one frame (in Bytes) varies according to bitrate. Eg. for 128kbps it is (normally) 417 Bytes and for 192kbps 626 Bytes. The first 4 Bytes of each frame is frame header and the rest is audio data.
Quoted from here.
And each frame is playable. So for 20
seconds of data, you need approximately 770 frames(20/0.026)
.
So download first 770
frames then cancel the download task. Then play your downloaded frames.
Or by size, download first 313.5 KB
(Calculation is: (417 Bytes*770)/1024
) of data for 128kbps file. And you will get your 20 seconds of data.
You would have to cut the audio file to 20 seconds, then you could download it.
I have also faced such challenge in one of my application. After, searching a lot I finally concluded it is not possible. As downloading an mp3
file has its own internal algorithm
which works according to frames.
So, we cannot predict anything regarding how much seconds or minutes of audio we have downloaded.
Further, you can refer this link.
Download last 30 seconds of an mp3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With