Xcode 声音选择器/macOS上的系统声音列表

Xcode 声音选择器/macOS上的系统声音列表,xcode,macos,cocoa,system-sounds,nssound,Xcode,Macos,Cocoa,System Sounds,Nssound,好的。我一直在寻找一种简洁的方法来列出使用[NSSound SoundName:]播放的系统声音——对我来说,API似乎没有可用声音的列表 我还用谷歌搜索了这篇文章,发现了一些非常古老的、部分已经弃用的信息源。如果应用程序应该有一个声音采集器,那么获取系统提供的声音列表的最佳方法是什么?这是我的实现 NSSound(systemSounds.h): 免费使用,可供任何人用于任何目的,如果您对其进行了增强,请共享:)使用Super检查所有系统声音!谢谢但是现在告诉我:怎么玩(其中一个)?我的意思是

好的。我一直在寻找一种简洁的方法来列出使用[NSSound SoundName:]播放的系统声音——对我来说,API似乎没有可用声音的列表


我还用谷歌搜索了这篇文章,发现了一些非常古老的、部分已经弃用的信息源。如果应用程序应该有一个声音采集器,那么获取系统提供的声音列表的最佳方法是什么?

这是我的实现

NSSound(systemSounds.h):


免费使用,可供任何人用于任何目的,如果您对其进行了增强,请共享:)

使用

Super检查所有系统声音!谢谢但是现在告诉我:怎么玩(其中一个)?我的意思是,我们有带名字的数组,但是如何(代码是什么)播放(比如“Glass.aiff”)?@Gik
[[NSSound soundNamed:@“Glass”]play]这里有一个Swift 5.1版本:这不是一个副本。另一个问题针对CocoaTouch/iOS,而不是询问系统声音列表。!
#import <AppKit/AppKit.h>

@interface NSSound (systemSounds)

+ (NSArray *) systemSounds;

@end
#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