Cannot download apple-music for using AVFoundation

95 Views Asked by At

I want to download music for iPhone "Music". I can select music in "Music" on real machine simulator.

I tried this proggram.

let songUrl = mediaItem.value(forProperty: MPMediaItemPropertyAssetURL) as! URL
        print(songUrl)

        // get file extension andmime type
        let str = songUrl.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: "")

        let exportSession = AVAssetExportSession(asset: AVAsset(url: songUrl), presetName: AVAssetExportPresetAppleM4A)
        exportSession?.shouldOptimizeForNetworkUse = true
        exportSession?.outputFileType = AVFileType.m4a

        //save it into your local directory
        let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let outputURL = documentURL.appendingPathComponent(mediaItem.title!)
        print(outputURL.absoluteString)
        print("おっけ")

        //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("Export Successfull")
                //  self.getAudio()
            }
            self.dismiss(animated: true, completion: nil)
        })

Show "Export Successfull" but not exist music(.m4a) in "Document" directory. I have No Error, so I'm in trouble...

1

There are 1 best solutions below

0
Guruguru On

I find at Window > Devices and Simulators!