2010-08-19 104 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;