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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10949138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:32:24  来源:igfitidea点击:

Sound picker / List of system sounds on macOS

xcodemacoscocoasystem-soundsnssound

提问by jake1981

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.

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

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?

我还用谷歌搜索了这个,发现了一些非常古老且部分已被弃用的资源。如果应用程序应该有一个声音选择器- 获取系统提供的声音列表的最佳方法是什么?

回答by jake1981

This is my implementation.

这是我的实现。

NSSound (systemSounds.h):

NSSound (systemSounds.h):

#import <AppKit/AppKit.h>

@interface NSSound (systemSounds)

+ (NSArray *) systemSounds;

@end

NSSound (systemSounds.m):

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 :)

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

回答by TUNER88

Check all system sounds with iOSSystemSoundsLibrary

使用iOSSystemSoundsLibrary检查所有系统声音