gpt4 book ai didi

ios - 如何从音乐库mediaPicker和文件应用documentPicker获取音频元数据?

转载 作者:行者123 更新时间:2023-11-28 14:28:21 29 4
gpt4 key购买 nike

在 Swift 4 中,我不知道如何在导入音频文件时从文档选取器获取音频元数据,以及在从用户媒体库中选取媒体项目时如何从媒体选取器获取音频元数据。我目前正在将 url 从两种导入方法转换为 AVAudioPlayer 项目。谁能告诉我一个方法,即使我必须为 mediaPickerdocumentPicker 分别编写代码?

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()
}

最佳答案

这不只是遵循文档的问题吗?

  1. 使用 URL 形成一个 AVAsset。

    https://developer.apple.com/documentation/avfoundation/avurlasset/1385698-init

  2. 现在提取元数据。

    https://developer.apple.com/documentation/avfoundation/avasset/1390498-commonmetadata

  3. 现在获取所需的元数据项:

    https://developer.apple.com/documentation/avfoundation/avmetadataitem/1385843-metadataitems

  4. 您现在拥有一个或多个 AVMetadataItem 对象。要检索值,请使用异步键值加载:

    https://developer.apple.com/documentation/avfoundation/avasynchronouskeyvalueloading

关于ios - 如何从音乐库mediaPicker和文件应用documentPicker获取音频元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411799/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com