How to download mp3 file from MPMediaPickerController using swift language?

1.2k Views Asked by At

My application is download song from MPMediaPickerController and save mp3 file in Document directory

File is download successfully in .m4a formate, But i need mp3 file. i'm already change file extension .m4a to .mp3 but this file is not playing.

I have tried a lot of near by examples but didn't succeed to solve this issue.

How can I do it?

 func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    mediaPicker.dismiss(animated: true) {

        let item: MPMediaItem = mediaItemCollection.items[0]
        let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
        if pathURL == nil {
            self.alert(message: "Sorry, the returned mediaItemCollection appeart to be empty")
            return
        }

        let song_Title = item.value(forProperty: MPMediaItemPropertyTitle) as! String
        let filename_song = song_Title.replacingOccurrences(of: " ", with: "_")

        // get file extension andmime type
        let str = pathURL!.absoluteString
        let str2 = str.replacingOccurrences( of : "ipod-library://item/item", with: "")
        let arr = str2.components(separatedBy: "?")
        var mimeType = arr[0]
        mimeType = mimeType.replacingOccurrences( of : ".", with: "")


        // Export the ipod library as .m4a file to local directory for remote upload
        let exportSession = AVAssetExportSession(asset: AVAsset(url: pathURL!), presetName: AVAssetExportPresetAppleM4A)
        exportSession?.shouldOptimizeForNetworkUse = true
        exportSession?.outputFileType = AVFileType.m4a
        exportSession?.metadata = AVAsset(url: pathURL!).metadata

        let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let outputURL = documentURL.appendingPathComponent("\(filename_song).m4a")

        //Delete Existing file
        do {
            try FileManager.default.removeItem(at: outputURL)
        } catch let error as NSError {
            print(error.debugDescription)
        }

        exportSession?.outputURL = outputURL
        exportSession?.exportAsynchronously(completionHandler: { () -> Void in

            if (exportSession!.status == AVAssetExportSession.Status.completed)
            {
                print("AV export succeeded.")
                self.alert(message: "File download successfully")

                do {

                    let newURL = outputURL.deletingPathExtension().appendingPathExtension("mp3")
                    let str = try FileManager.default.moveItem(at: outputURL, to: newURL)

                    print(str)
                } catch {
                    print("The file could not be loaded")
                }
            }
            else if (exportSession!.status == AVAssetExportSession.Status.cancelled)
            {
                print("AV export cancelled.")
            }
            else
            {
                print("AV export failed with error:- ", exportSession!.error!.localizedDescription)
            }

        })

    }
}
0

There are 0 best solutions below