如何使用 Swift 代码访问媒体库?

How to access the media library with Swift code?

我必须显示我选择的歌曲的标题我得到了标题(不需要播放音乐库中的歌曲)。我的问题是我想发送我选择的歌曲(编码)到 PHP 服务器..要在服务器的 table 视图中显示歌曲并播放歌曲...我想找到 iTunes 音乐库的正确路径,以便我可以轻松编码。

func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems      mediaItemCollection: MPMediaItemCollection) {

    let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

    do {
        // Get the directory contents urls (including subfolders urls)
        let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL( documentsUrl, includingPropertiesForKeys: nil, options: [])

        let mp3Files = directoryContents.filter{ [=11=].pathExtension == "m4a" }
        let mp3FileNames = mp3Files.flatMap({[=11=].URLByDeletingPathExtension?.lastPathComponent})

    } catch let error as NSError {
    }
    let name = "/aFileName"
    var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first
    print("\nfilePath: \(filePath)")

    filePath = filePath!.stringByAppendingString(name)
    print("\nfilePath: \(filePath)")

    var filePathURL = NSURL.fileURLWithPath(filePath!)
    print("\nfilePathURL: \(filePathURL)")


    let item: MPMediaItem = mediaItemCollection.items[0]
    print(item)
    exportFiles = (item.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL)!
    print(exportFiles)        
    // Export the ipod library as .m4a file to local directory for remote upload
    let exportSession = AVAssetExportSession(asset: AVAsset(URL: exportFiles), presetName: AVAssetExportPresetAppleM4A)
    print(exportFiles)

    exportSession?.shouldOptimizeForNetworkUse = true
    print(exportSession)

    playingMusictitle = item.valueForProperty(MPMediaItemPropertyTitle) as? String ?? "Now Playing..."
    print("\(exportFiles), title : \(title) ")
    let str = exportFiles.absoluteString
    let str2 = str!.stringByReplacingOccurrencesOfString("ipod-library://item/item", withString: "")
    print(str2)

    let arr = str2.componentsSeparatedByString("?")


    mimeType = mimeType1.stringByReplacingOccurrencesOfString("id=", withString: "")


    let path = item.valueForProperty(MPMediaItemPropertyLyrics) as? String ?? ""
    print(path)


    exportSession?.outputFileType = AVFileTypeAppleM4A
    let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]

    let format = NSDateFormatter()
    format.dateFormat="yyyy-MM-dd-HH-mm-ss"
    audioname = "\(playingMusictitle)-\(format.stringFromDate(NSDate())).m4a"
    print(audioname)

    let documentsDirectoryy = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
    self.soundFileURL = documentsDirectoryy.URLByAppendingPathComponent(audioname)

    if NSFileManager.defaultManager().fileExistsAtPath(soundFileURL.absoluteString!) {
        // probably won't happen. want to do something about it?
        print("soundfile \(soundFileURL.absoluteString) exists")
    }

由于版权保护,您只能播放曲库中的歌曲。您无法访问行音频文件!我猜