声音选择器/macOS上的系统声音列表 [英] Sound picker / List of system sounds on macOS

查看:172
本文介绍了声音选择器/macOS上的系统声音列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我还用Google搜索了此文件,发现了一些相当古老且已部分弃用的资源.如果应用程序应具有声音选择器-获取系统提供的声音列表的最佳方法是什么?

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?

推荐答案

这是我的实现.

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

这篇关于声音选择器/macOS上的系统声音列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆