如何创建SystemSoundID的数组

时间:2023-02-02 19:15:08

I cannot seem to figure it out.

我好像弄不明白。

I can play one sound with:

我可以播放一个声音:

- (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);
}

But I want to create an array of SystemSoundID objects. When I try to do this I keep getting errors. Can anyone show me the code to create an array of soundFileObjects and how I use that array in AudioServicesPlaySystemSound?

但我想创建一个SystemSoundID对象数组。当我尝试这样做时,我总是会出错。谁能告诉我创建一个soundfileobject数组的代码,以及我如何在AudioServicesPlaySystemSound中使用该数组吗?

1 个解决方案

#1


7  

SystemSoundID is an integral, non-class, type, so you'd need to use a wrapper like NSNumber to store it in Cocoa containers like NSArray:

SystemSoundID是一个完整的,非类,类型,所以需要使用NSNumber这样的包装器将它存储在NSArray这样的Cocoa容器中:

// 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);

#1


7  

SystemSoundID is an integral, non-class, type, so you'd need to use a wrapper like NSNumber to store it in Cocoa containers like NSArray:

SystemSoundID是一个完整的,非类,类型,所以需要使用NSNumber这样的包装器将它存储在NSArray这样的Cocoa容器中:

// 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);