iOS 6 issue Convert MPMediaItem to NSData

后端 未结 3 1282
星月不相逢
星月不相逢 2021-02-04 17:42

i had tried the below code.

   -(void)mediaItemToData : (MPMediaItem * ) curItem
{
    NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];

            


        
3条回答
  •  时光取名叫无心
    2021-02-04 18:22

    Try this one written in Swift4 and reason of using AVAssetExportSession https://stackoverflow.com/a/36694392/5653015

    func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection)
    {
        //get media item first
        guard let mediaItem = mediaItemCollection.items.first else
        {
            NSLog("No item selected.")
            return
        }
    
    
        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)
        //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 == AVAssetExportSessionStatus.completed
            {
                print("Export Successfull")
              //  self.getAudio()
            }
            self.dismiss(animated: true, completion: nil)
        })
    }
    

提交回复
热议问题