2010-08-19 87 views
1

我有这个代码来播放声音,但第一次玩它需要花5秒钟的时间加载...可可触摸 - 加载AVAudioPlayer

我怎样才能加快速度呢?

-(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]; 
} 

回答

0

你可以把你的代码的以下部分中viewDidLoad尝试,以便audioPlayer实例准备打之前有人问到实际播放音频:

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;