iOS 6 issue Convert MPMediaItem to NSData

后端 未结 3 1279
星月不相逢
星月不相逢 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:07

    SWIFT 3 Version from @Hitarth

    func save(mediaItem: MPMediaItem) -> String? {
        let url = mediaItem.assetURL!
        let hex = MD5Hex(string: url.absoluteString)
        guard didMD5HexExist(string: hex) == false else {
            return hex
        }
        let songAsset = AVURLAsset(url: url)
        guard let exporter = AVAssetExportSession(asset: songAsset, presetName: AVAssetExportPresetAppleM4A) else {
            assertionFailure()
            return nil
        }
        exporter.outputFileType = "com.apple.m4a-audio"
    
        let fileHexName = hex + ".m4a"
        let fileURL = latFileManager.getResourceFolderPath().appendingPathComponent(fileHexName)
        DLog("fileURL: \(fileURL)")
    
        exporter.outputURL = fileURL
        // do the export
        exporter.exportAsynchronously {
            let status = exporter.status
            switch status {
            case .failed:
                assertionFailure(exporter.error as! String)
            case .completed:
                DLog("AVAssetExportSessionStatusCompleted")
                self.fileHexArray.append(fileHexName)
            default:
                DLog("default")
                break
            }
        }
        return hex
    }
    

提交回复
热议问题