我正在开发Swift3中的音乐应用程序。我需要从设备库中选择任何音频歌曲并将其上传到Amazon S3。这是我的代码:
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection)
{
self.dismiss(animated: true, completion: nil)
let item: MPMediaItem? = (mediaItemCollection.items[0] as? MPMediaItem)
let url: URL? = item?.value(forProperty: MPMediaItemPropertyAssetURL) as! URL?
let albumTitle = item?.value( forProperty: MPMediaItemPropertyTitle ) as! String
let albumMediaType = item?.value( forProperty: MPMediaItemPropertyMediaType) as! Int
}
作为输出,我得到资产网址:ipod-library://item/item.mp3?id = 8739819345820495362媒体类型将以整数1/2/3/4进行....
现在,我的问题是我需要知道所选歌曲的文件类型,因为当我将硬编码'.mp3'添加到文件并将其上传到Amazon S3时,一些音频文件正在播放而休息不是!
你能帮我解决这个问题吗?谢谢。
编辑:这是我将AssetUrl转换为路径的代码:
func exportMedia(_ assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -> ()) {
let asset = AVURLAsset(url: assetURL)
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else {
completionHandler(nil, ExportError.unableToCreateExporter)
return
}
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(NSUUID().uuidString)
.appendingPathExtension("m4a")
exporter.outputURL = fileURL
exporter.outputFileType = "com.apple.m4a-audio"
exporter.exportAsynchronously {
if exporter.status == .completed {
completionHandler(fileURL, nil)
} else {
completionHandler(nil, exporter.error)
}
}
}