如何使用快速语言从MPMediaPickerController下载mp3文件?

时间:2019-01-24 10:15:10

标签: ios swift mpmediapickercontroller

我的应用程序是从MPMediaPickerController下载歌曲并将mp3文件保存在文档目录中

文件已成功在.m4a formate中下载,但是我需要mp3文件。我已经将文件扩展名.m4a更改为.mp3,但是此文件无法播放。

我已经通过实例进行了很多尝试,但是并没有成功解决这个问题。

我该怎么办?

 func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
    mediaPicker.dismiss(animated: true) {

        let item: MPMediaItem = mediaItemCollection.items[0]
        let pathURL: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL
        if pathURL == nil {
            self.alert(message: "Sorry, the returned mediaItemCollection appeart to be empty")
            return
        }

        let song_Title = item.value(forProperty: MPMediaItemPropertyTitle) as! String
        let filename_song = song_Title.replacingOccurrences(of: " ", with: "_")

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


        // 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 = AVFileType.m4a
        exportSession?.metadata = AVAsset(url: pathURL!).metadata

        let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let outputURL = documentURL.appendingPathComponent("\(filename_song).m4a")

        //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("AV export succeeded.")
                self.alert(message: "File download successfully")

                do {

                    let newURL = outputURL.deletingPathExtension().appendingPathExtension("mp3")
                    let str = try FileManager.default.moveItem(at: outputURL, to: newURL)

                    print(str)
                } catch {
                    print("The file could not be loaded")
                }
            }
            else if (exportSession!.status == AVAssetExportSession.Status.cancelled)
            {
                print("AV export cancelled.")
            }
            else
            {
                print("AV export failed with error:- ", exportSession!.error!.localizedDescription)
            }

        })

    }
}

0 个答案:

没有答案