【问题标题】:Adding ipod music files to bundles files in swift快速将 ipod 音乐文件添加到捆绑文件中
【发布时间】:2016-08-28 11:12:57
【问题描述】:

我正在制作一个应用程序,我希望它从 iPod 音乐文件中挑选一首歌曲,然后将其添加到捆绑文件(文档文件夹)中,这样我就可以在需要再次访问手机音乐列表时使用它。

我已经设法挑选了歌曲并获得了它的 ID,但我不知道如何使用 Swift 将文件添加到我的捆绑文件中。

我找到了与这个问题有点相似的答案,但都是从 iOS 4 开始,并且使用的是 Objective C 而不是 Swift。

这是具有文件 ID 的代码:

let u: NSURL? = 
        self.mediaItem!.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL?
if u == nil {
    return
}

【问题讨论】:

    标签: iphone swift xcode


    【解决方案1】:

    我的答案来源是here我没有测试代码

    func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
        let item: MPMediaItem = mediaItemCollection.items[0]
        let pathURL: NSURL? = item.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL
        if pathURL == nil {
            Alert.showPopupWithMessage("Unable to read DRM protected file.")
            return
        }
    
        // 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 = AVFileTypeAppleM4A
        let fileUrl = //Where you want to save the file
        exportSession?.outputURL = fileUrl
        exportSession?.exportAsynchronouslyWithCompletionHandler({ () -> Void in
            if exportSession!.status != AVAssetExportSessionStatus.Completed  {
                print("Export error")
            }
        })
    }
    

    【讨论】:

    • 好的,所以回答了我的问题的一半,但另一半是用于访问包中的文档文件夹或访问包本身并将文件保存到其中的代码是什么?
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多