In my app I have 52 mp3 and when I load my view controller I alloc all 52 mp3 in this way:
NSString *pathFrase1 = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],[NSString stringWithFormat:@"a%d_1a.mp3",set]];
NSURL *filePath1 = [NSURL fileURLWithPath:pathFrase1 isDirectory:NO];
f1 = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath1 error:nil];
[f1 prepareToPlay];
but it’s very slow when I open viewcontroller, then is there a way to alloc mp3 when I use it? and release its AVAudioPlayer??
Sure, but there will be a delay as the
AVAudioPlayeris allocated and prepared for playing. Can you predict what will play and when? If so, maybe you can load a couple of seconds before you need a particular mp3 to play.An alternative, which may not work depending on your timing requirements, is the following:
This solution requires properties of
FileNamesandAudioPlayers.Once this is set up, you could do something like the following (probably in
viewDidLoad):Later, when you need to play a particular file, you can find the index of the file name in the
FileNamesarray and the callplayon theAVAudioPlayerfor that index in theAudioPlayersarray.This seems like maybe not the best way to do things, but it might work if you require it this way.