开发者

Cocoa Touch - Loading AVAudioPlayer

开发者 https://www.devze.com 2023-01-12 02:04 出处:网络
I have this code to play a sound but the first time you play it it takes a good 5 seconds to load... How can I speed this up?

I have this code to play a sound but the first time you play it it takes a good 5 seconds to load...

How can I speed this up?

-(IBAction)playSound{ //play the crick开发者_StackOverflow中文版et 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];
}


You can try by putting the following section of your code in viewDidLoad so that the audioPlayer instance is ready to play before it is asked to actually play the 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消