在Swift 4中,我无法弄清楚在导入音频文件时如何从文档选择器获取音频元数据,以及从用户的媒体库中选择媒体项时如何从媒体选择器获取音频元数据。目前,我正在将两种导入方法的网址都转换为AVAudioPlayer
项。有人可以让我知道一个方法,即使我必须分别为mediaPicker
和documentPicker
进行编码吗?
func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
for song in mediaItemCollection.items as [MPMediaItem] {
// AVAudioPlayer
let url = song.value(forProperty: MPMediaItemPropertyAssetURL) as? NSURL
audioPlayer = try? AVAudioPlayer(contentsOf: url! as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
mediaPicker.dismiss(animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
guard controller.documentPickerMode == .import else { return }
let fileURL = url
let url = url.deletingPathExtension()
audioPlayer = try? AVAudioPlayer(contentsOf: fileURL)
audioPlayer.enableRate = true
audioPlayer.prepareToPlay()
audioPlayer.play()
updateCurrentTrack()
}
答案 0 :(得分:1)
不仅仅是跟随文档的问题吗?
使用URL形成AVAsset。
https://developer.apple.com/documentation/avfoundation/avurlasset/1385698-init
现在提取元数据。
https://developer.apple.com/documentation/avfoundation/avasset/1390498-commonmetadata
现在获得所需的元数据项:
https://developer.apple.com/documentation/avfoundation/avmetadataitem/1385843-metadataitems
您现在具有一个或多个AVMetadataItem对象。要检索值,请使用异步键值加载:
https://developer.apple.com/documentation/avfoundation/avasynchronouskeyvalueloading