2010-07-10 74 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); 
相關問題