如何找到Lion 10.7中AVAudioPlayer支持的所有格式的音频文件?

发布于 2024-11-30 15:13:36 字数 481 浏览 0 评论 0原文

我想使用这样的代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南…巷孤猫 2024-12-07 15:13:37

使用 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.

伴梦长久 2024-12-07 15:13:36

要获取最支持的格式列表,您可以使用 AudioToolbox 框架中的 AudioFileGetGlobalInfo 来获取 Core Audio 支持的 UTI(使用 kAudioFileGlobalInfo_AllUTIs) :

UInt32 size;
NSArray *all;
OSStatus err;

err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AllUTIs, 0, NULL, &size);
if (err == noErr)
    err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllUTIs, 0, NULL, &size, &all);
if (err == noErr)
    NSLog(@"UTIs: %@", all);
[all release];

在 10.7 上,这给了我:

"public.aiff-audio",
"public.ulaw-audio",
"org.3gpp.adaptive-multi-rate-audio",
"com.microsoft.waveform-audio",
"public.3gpp2",
"com.apple.coreaudio-format",
"public.3gpp",
"public.mp3",
"public.au-audio",
"public.mpeg-4-audio",
"public.mpeg-4",
"com.apple.m4a-audio",
"public.aifc-audio"

不幸的是,UTI 没有为一些更晦涩的数据格式定义(例如.mp1/.mp2) Core Audio 支持;如果您对上述子集感到满意,则只需使用 UTI。

然后将它们转换为 NSMetadataQuerykMDItemContentType for kAudioFileGlobalInfo_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 the AudioToolbox framework to get the UTIs supported by Core Audio (using kAudioFileGlobalInfo_AllUTIs):

UInt32 size;
NSArray *all;
OSStatus err;

err = AudioFileGetGlobalInfoSize(kAudioFileGlobalInfo_AllUTIs, 0, NULL, &size);
if (err == noErr)
    err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllUTIs, 0, NULL, &size, &all);
if (err == noErr)
    NSLog(@"UTIs: %@", all);
[all release];

On 10.7, this gives me:

"public.aiff-audio",
"public.ulaw-audio",
"org.3gpp.adaptive-multi-rate-audio",
"com.microsoft.waveform-audio",
"public.3gpp2",
"com.apple.coreaudio-format",
"public.3gpp",
"public.mp3",
"public.au-audio",
"public.mpeg-4-audio",
"public.mpeg-4",
"com.apple.m4a-audio",
"public.aifc-audio"

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 for kAudioFileGlobalInfo_AllUTIs). If you want to cover the rest of the formats, you can match by HFS type and extension: kMDItemFSTypeCode for kAudioFileGlobalInfo_AllHFSTypeCodes, and a wildcard match of kMDItemFSName for kAudioFileGlobalInfo_AllExtensions. You can use afconvert -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 checking kMDItemAudioBitRate 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文