LA1
LA1

Reputation: 27

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) [oneAudio release];
    NSError *error = nil;
    oneAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
    if (error)
        NSLog(@"%@",[error localizedDescription]);
    oneAudio.delegate = self;
    [oneAudio play];    


}

thanks

Upvotes: 0

Views: 402

Answers (2)

Monkeyanator
Monkeyanator

Reputation: 1416

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.

Upvotes: 0

Brett
Brett

Reputation: 2635

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.

Upvotes: 1

Related Questions