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

标签 xcode macos cocoa system-sounds nssound

好的。我一直在寻找一种简洁的方法来列出使用 [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/

相关文章:

objective-c - 在哪里定义了像 NSControlKeyMask 这样的标识符

ios - fatal error : init(coder:) has not been implemented Xcode 7 iOS 9

swift - 如何从 xcode 中 Storyboard上的导航 Controller 导航回来

ios - 重命名伞头

swift - 如何在 swift macOS 中旋转 PDF 页面?

python - 如何为 Python 脚本定义系统范围的别名?

swift - 如何防止 NSCursor 发生变化?

iphone - 如何减小 iPhone 应用程序的大小?

ios - 默认今日小部件中的布局约束冲突

objective-c - 使用 NSAlert 处理按下哪个按钮的正确方法是什么