ios - 如何在 iOS 11 上的 Swift 中获取 FLAC 文件元数据?
问题描述
我需要获取 FLAC 文件的元数据。我尝试了以下代码:
let item = AVPlayerItem(url: URL(fileURLWithPath: path))
let commonMetadata = item.asset.commonMetadata
songInfo[ARTIST_NAME] = "Unknown"
songInfo[GENRE_NAME] = "Unknown"
songInfo[ALBUM_NAME] = "Unknown"
songInfo[PLAY_COUNT] = "0"
for metadataItem in commonMetadata {
switch metadataItem.commonKey?.rawValue ?? "" {
case "type":
songInfo[GENRE_NAME] = metadataItem.stringValue
case "albumName":
songInfo[ALBUM_NAME] = metadataItem.stringValue
case "artist":
songInfo[ARTIST_NAME] = metadataItem.stringValue
default: break
}
}
但这不适用于 FLAC 文件。任何帮助将不胜感激。
解决方案
只需使用AudioToolbox API:
func audioFileInfo(url: URL) -> NSDictionary? {
var fileID: AudioFileID? = nil
var status:OSStatus = AudioFileOpenURL(url as CFURL, .readPermission, kAudioFileFLACType, &fileID)
guard status == noErr else { return nil }
var dict: CFDictionary? = nil
var dataSize = UInt32(MemoryLayout<CFDictionary?>.size(ofValue: dict))
guard let audioFile = fileID else { return nil }
status = AudioFileGetProperty(audioFile, kAudioFilePropertyInfoDictionary, &dataSize, &dict)
guard status == noErr else { return nil }
AudioFileClose(audioFile)
guard let cfDict = dict else { return nil }
let tagsDict = NSDictionary.init(dictionary: cfDict)
return tagsDict
}
示例输出:
- 0 : 2 elements
* key : artist
* value : Blue Monday FM
- 1 : 2 elements
* key : title
* value : Bee Moved
- 2 : 2 elements
* key : album
* value : Bee Moved
- 3 : 2 elements
* key : approximate duration in seconds
* value : 39.876
- 4 : 2 elements
* key : source encoder
* value : reference libFLAC 1.2.1 win64 200807090
推荐阅读
- mathjax - 我怎样才能中断变量名?
- python - 如何预测 Keras 中每个序列的多个值?LSTM 层
- iframe - 第三方重定向在 iframe 内的 Samesite Lax 预期行为
- c - 立即复制宏中的文本
- javascript - 两个手风琴,但仅使用 Javascript 为第一个箭头设置动画
- c++ - 为什么下面这段代码返回的是指针指向的值而不是指针的地址?
- c++ - 从 GUI 文件资源管理器执行程序,并且不生成终端
- c++ - 有没有办法使用 VFW 将压缩的视频数据写入缓冲区而不是文件?
- xcode - _shouldMakeReadyForDevelopment 检查失败,即使设备没有被密码锁定
- javascript - 使用 cts.fieldValueMatch 搜索 dateTime 数据类型的通配符