How to put sounds in an array?
I have 10 sounds, and it would be easier for me to put them into an array. However I'm new to Obj C.
Here is the code so far, this is just for 1 sound. I could copy and paste it 10 times. But if all 10 sounds are in an array I'll be able to do more functions with the sounds.
- (IBAction)oneSound:(id)sender; {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Vocal 1" ofType:@"mp3"];
if (oneAudio) [oneA开发者_如何学运维udio release];
NSError *error = nil;
oneAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(@"%@",[error localizedDescription]);
oneAudio.delegate = self;
[oneAudio play];
}
thanks
Heres one way,
NSMutableArray *soundArray = [NSMutableArray arrayWithCapacity:10];
[soundArray addObject:oneAudio];
.
.
.
[soundArray addObject:tenAudio];
or you could also put the paths to the audio content into the Array and instantiate them when required.
Take the file paths of the arrays, make them into strings, add THOSE to the array, and access them only when needed, or else you'll be using up a lot of memory.
精彩评论