问题
Okay. I was looking for a neat way to list system sounds that are played with [NSSound soundNamed: ] - and to me it seemed that API does not have list of available sounds.
I also searched this with google and found some pretty old and partially already deprecated sources. If application should have a soundpicker - what would be a best way to get list of system provided sounds?
回答1:
This is my implementation.
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
Freely usable and available for anyone for any purpose and if you enhance it, please share :)
回答2:
Check all system sounds with iOSSystemSoundsLibrary
来源:https://stackoverflow.com/questions/10949138/sound-picker-list-of-system-sounds-on-macos