如何从音乐库mediaPicker和文件应用documentPicker获取音频元数据?
How to obtain audio metadata from music library mediaPicker and the Files app documentPicker?
在 Swift 4 中,我无法弄清楚如何在导入音频文件时从文档选取器获取音频元数据,以及在从用户媒体库中选取媒体项目时如何从媒体选取器获取音频元数据。我目前正在将 url 从两种导入方法转换为 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()
}
不就是看文档的问题吗?
使用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
在 Swift 4 中,我无法弄清楚如何在导入音频文件时从文档选取器获取音频元数据,以及在从用户媒体库中选取媒体项目时如何从媒体选取器获取音频元数据。我目前正在将 url 从两种导入方法转换为 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()
}
不就是看文档的问题吗?
使用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