2010-07-10 52 views
3

我似乎无法弄清楚。我该如何制作一组SystemSoundID

我可以打一个声音:

- (void) initSounds { 
    // Get the main bundle for the app 
    CFBundleRef mainBundle; 
    mainBundle = CFBundleGetMainBundle(); 
    // Get the URL to the sound file to play 
    soundFileURLRef = CFBundleCopyResourceURL (mainBundle,CFSTR ("1"),CFSTR ("wav"),NULL); 

    // Create a system sound object representing the sound file 
    AudioServicesCreateSystemSoundID (soundFileURLRef, &soundFileObject); 
} 

-(void) play { 
    AudioServicesPlaySystemSound(self.soundFileObject); 
} 

但我想创建SystemSoundID对象的数组。当我尝试这样做时,我不断收到错误。任何人都可以向我展示创建一个soundFileObjects数组的代码,以及我如何在AudioServicesPlaySystemSound中使用该数组?

+0

我贴使用对象[这里的备选答案] [1]。 [1]:http://stackoverflow.com/a/26701297/951349 – smileBot 2014-11-03 18:17:59

回答

7

SystemSoundID是不可分割的,非类,类型,所以你需要使用的包装像NSNumber将其存储在可可容器,如NSArray

// assuming this property: 
@property (copy) NSArray *soundFileObjects; 

// creating the array: 
soundFileObjects = [[NSArray alloc] initWithObjects: 
        [NSNumber numberWithUnsignedLong:soundId], 
        // more ? 
        nil]; 

// using it, e.g.: 
SystemSoundID sid = [[self.soundFileObjects objectAtIndex:0] unsignedLongValue]; 
AudioServicesPlaySystemSound(sid); 
相关问题