参见英文答案 >
Playing system sound without importing your own 9个
好的.我正在寻找一种简洁的方法来列出用[NSSound soundNamed:]播放的系统声音 – 对我而言似乎API没有可用声音列表.
好的.我正在寻找一种简洁的方法来列出用[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
可自由使用,可供任何人用于任何目的,如果你增强它,请分享:)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。