In Swift 4, I can't figure out how to obtain audio metadata from the document picker when importing audio files and from the media picker when picking a media item from the user's media library. I am currently converting the url from both import methods to an AVAudioPlayer
item. Can someone please let me know a method, even if I have to code it separately for both the mediaPicker
and the 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()
}