2010-08-19 6 views
1

J'ai ce code pour jouer un son, mais la première fois que vous jouez il faut un bon 5 secondes pour charger ...Cocoa Touch - Chargement AVAudioPlayer

Comment puis-je accélérer ce?

-(IBAction)playSound{ //play the cricket noise 
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"]; 
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL]; 
audioPlayer.delegate = self; 
[audioPlayer prepareToPlay]; 
audioPlayer.numberOfLoops = 0; 
[audioPlayer play]; 
} 

Répondre

0

Vous pouvez essayer en mettant la section suivante de votre code viewDidLoad afin que l'instance audioPlayer est prêt à jouer avant qu'il ne soit demandé de jouer effectivement l'audio:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Cricket_Sound" ofType:@"mp3"]; 
audioPlayer =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundPath] error:NULL]; 
audioPlayer.delegate = self; 
[audioPlayer prepareToPlay]; 
audioPlayer.numberOfLoops = 0;