Friday, May 8, 2009

Playing two or more sounds using AVAudioPlayer

For performance, read the sound data into NSData;

NSString* pathOfSound = [[NSBundle mainBundle] pathForResource:@"mySound" ofType:@"caf"];
NSData* dataOfSound = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathOfSound]];


Then, whenever you want to play sound, create a new object of AVAudioPlayer.


AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithData:dataOfSound error:NULL];
player.delegate = self; // for releasing object, see "Playing sound with AVAudioPlayer"
[player play];

No comments:

Post a Comment