2012-06-08 4 views
3

오케이. [NSSound soundNamed :]와 함께 연주되는 시스템 사운드를 나열하는 깔끔한 방법을 찾고있었습니다. API에 사용 가능한 사운드 목록이없는 것 같았습니다.사운드 픽커/시스템 사운드 목록

나는 또한 이것을 구글로 검색했으며 꽤 오래된 부분적으로 이미 사용되지 않는 소스를 발견했다. 애플리케이션에 의 사운드 피커가 있어야합니다. - 시스템에서 제공하는 사운드 목록을 얻는 가장 좋은 방법은 무엇입니까?

답변

9

이것은 구현 한 것입니다.

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 
자유롭게 사용 가능하고 어떤 목적을 위해 당신이 그것을 향상 경우 누구에게나 제공

공유하십시오 :)

+0

최고! 감사. 그러나 이제 나에게 말해라. (그들 중 하나) 어떻게 놀 수 있니? 우리는 이름을 가진 배열을 가지고 있지만, (코드는 무엇입니까?) 어떻게 연주해야합니까 ("Glass.aiff"라고 말하고 싶습니까?). – Gik

+2

@Gik'[[NSSound soundNamed : @ "유리"] 놀이];' –

관련 문제