Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - download only few seconds from mp3 file

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.

like image 315
Eyal Avatar asked Mar 15 '17 15:03

Eyal


People also ask

How to download MP3 music from web to iPhone?

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.

How to transfer MP3 files from iTunes to iPhone without iTunes?

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.

How to download music from Google Music Play to 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.

How to transfer files from computer to iPhone easily?

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.


3 Answers

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.

like image 174
Partho Biswas Avatar answered Oct 12 '22 03:10

Partho Biswas


You would have to cut the audio file to 20 seconds, then you could download it.

like image 27
Jamie Lipper Avatar answered Oct 12 '22 05:10

Jamie Lipper


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

like image 2
Arpit Jain Avatar answered Oct 12 '22 04:10

Arpit Jain