好的。我一直在寻找一种简洁的方法来列出使用 [NSSound soundNamed: ] 播放的系统声音 - 对我来说,API 似乎没有可用声音列表。
我还用谷歌搜索了这个,发现了一些相当古老且部分已弃用的来源。如果应用程序应该有声音选择器 - 获取系统提供的声音列表的最佳方法是什么?
最佳答案
这是我的实现。
NSSound(systemSounds.h):
#import <AppKit/AppKit.h>
@interface NSSound (systemSounds)
+ (NSArray *) systemSounds;
@end
NSSound(systemSounds.m):
#import "NSSound (systemSounds).h"
@implementation NSSound (systemSounds)
static NSArray *systemSounds = nil;
+ (NSArray *) systemSounds
{
if ( !systemSounds )
{
NSMutableArray *returnArr = [[NSMutableArray alloc] init];
NSEnumerator *librarySources = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES) objectEnumerator];
NSString *sourcePath;
while ( sourcePath = [librarySources nextObject] )
{
NSEnumerator *soundSource = [[NSFileManager defaultManager] enumeratorAtPath: [sourcePath stringByAppendingPathComponent: @"Sounds"]];
NSString *soundFile;
while ( soundFile = [soundSource nextObject] )
if ( [NSSound soundNamed: [soundFile stringByDeletingPathExtension]] )
[returnArr addObject: [soundFile stringByDeletingPathExtension]];
}
systemSounds = [[NSArray alloc] initWithArray: [returnArr sortedArrayUsingSelector:@selector(compare:)]];
[returnArr release];
}
return systemSounds;
}
@end
任何人都可以出于任何目的免费使用,如果您增强了它,请分享:)
关于xcode - 声音选择器/macOS 上的系统声音列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10949138/