mardi 20 janvier 2015

Xcode pre-load many audio files into one AVAudioPLayer?

I managed to load an audio file onto a player and play it via the following method:



- (void) play:(NSString*)loop{

NSString* resourcePath = [[NSBundle mainBundle] pathForResource:loop
ofType:@"mp3"];



player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath]];

player.delegate = self;
[player play];
player.numberOfLoops = 1;
player.currentTime = 0;
player.volume = 1.0;
}
}


(I removed a couple of lines in this post but the method itself works great)


The problem here is, that the sound only gets loaded when the method play() is being called which results in a little delay in between the button click that calls the method and the player actually playing it.


I have about 200 sounds in my app and one way would be to load each sound in its own AVAudioPLayer in the viewDidLoad, but I doubt that this is considered good code.


On Android, I am using the SoundPool for this concern and it works great but I don't think there is an equivalent for this on iOS.


Aucun commentaire:

Enregistrer un commentaire