如何找到Lion 10.7中AVAudioPlayer支持的所有格式的音频文件?
我想使用这样的代码。
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes: [NSArray arrayWithObject: [NSURL fileURLWithPath:@"/Users/Someone/Music" isDirectory:YES]]];
[query setPredicate: predicate];
...
...
现在我该如何设置“谓词”来过滤掉那些格式不受支持的文件?
kMDItemCodezs,kMDItemMediaTypes,kMDItemContentType,kMDItemKind?
我应该使用哪一个? Lion 10.7 中 AVAudioPlayer 支持的格式对应的这些属性的所有可能值是什么?多谢。
I want to use codes like this.
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes: [NSArray arrayWithObject: [NSURL fileURLWithPath:@"/Users/Someone/Music" isDirectory:YES]]];
[query setPredicate: predicate];
...
...
Now how do I suppose to set "predicate" to filter out those files with unsupported format??
kMDItemCodezs,kMDItemMediaTypes,kMDItemContentType,kMDItemKind?
Which one should I use? And what are all the possible values of these attibutes corresponding to the supported format in AVAudioPlayer in Lion 10.7? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 kMDItemContentTypeTree 和 音频类型。这将匹配任何音频文件,忽略电影文件。如果要包含电影文件,请搜索 视听内容类型,音频类型符合该类型(源自)。
编辑:这将匹配所有已知的音频类型,无论Core Audio(更不用说AVAudioPlayer)是否可以播放它们。尼古拉斯·莱利的解决方案更好。
Use
kMDItemContentTypeTree
, and the audio type. This will match any audio files, ignoring movie files. If you want to include movie files, search for the audio-visual content type, which the audio type conforms to (descends from).Edit: This will match all known audio types, regardless of whether Core Audio (let alone AVAudioPlayer) can play them. Nicholas Riley's solution is better.
要获取最支持的格式列表,您可以使用
AudioToolbox
框架中的AudioFileGetGlobalInfo
来获取 Core Audio 支持的 UTI(使用kAudioFileGlobalInfo_AllUTIs
) :在 10.7 上,这给了我:
不幸的是,UTI 没有为一些更晦涩的数据格式定义(例如
.mp1
/.mp2
) Core Audio 支持;如果您对上述子集感到满意,则只需使用 UTI。然后将它们转换为
NSMetadataQuery
(kMDItemContentType
forkAudioFileGlobalInfo_AllUTIs
)。如果您想覆盖其余格式,可以按 HFS 类型和扩展名进行匹配:kMDItemFSTypeCode
表示kAudioFileGlobalInfo_AllHFSTypeCodes
,以及kMDItemFSName
的通配符匹配> 对于kAudioFileGlobalInfo_AllExtensions
。您可以使用 afconvert -hf 来显示这两个内容。与 NSMetadataQuery 匹配当然不会查看所有文件的内部,因此它仍然会找到使用
.mp3
扩展名重命名的文本文件。由于 Spotlight 确实会尝试索引其他音频属性,因此您可以尝试检查 kMDItemAudioBitRate 等;这些在实际上不是音频文件的文件中将会丢失。根据您想要过滤的准确程度,您还可以尝试打开每个文件以查看其是否可以播放。To obtain a list of most supported formats, you can use
AudioFileGetGlobalInfo
in theAudioToolbox
framework to get the UTIs supported by Core Audio (usingkAudioFileGlobalInfo_AllUTIs
):On 10.7, this gives me:
Unfortunately UTIs aren't defined for some of the more obscure data formats (e.g.
.mp1
/.mp2
) Core Audio supports; if you're happy with the above subset, then just use the UTIs.Then turn those into a
NSMetadataQuery
(kMDItemContentType
forkAudioFileGlobalInfo_AllUTIs
). If you want to cover the rest of the formats, you can match by HFS type and extension:kMDItemFSTypeCode
forkAudioFileGlobalInfo_AllHFSTypeCodes
, and a wildcard match ofkMDItemFSName
forkAudioFileGlobalInfo_AllExtensions
. You can useafconvert -hf
to display both of these.Matching with
NSMetadataQuery
will of course not look inside all of the files, so it'll still find text files renamed with a.mp3
extension. Since Spotlight does try to index other audio attributes, you could try checkingkMDItemAudioBitRate
and so forth; these will be missing on a file that isn't actually an audio file. Depending on how accurate you want to be in filtering, you can also try opening each file to see if it's playable.