我正在使用iOS SDK中的AVAudioPlayer在每次点击tableView行时播放短音。 我已经手动在每一行的按钮上启动了@selector,它启动了方法playSound:(id)receiver {}。从接收器我得到声音的URL,所以我可以播放它。iPhone AVAudioPlayer应用程序在第一次播放时冻结
这种方法看起来像这样:
- (void)playSound:(id)sender {
[audioPlayer prepareToPlay];
UIButton *audioButton = (UIButton *)sender;
[audioButton setImage:[UIImage imageNamed:@"sound_preview.png"] forState:UIControlStateNormal];
NSString *soundUrl = [[listOfItems objectForKey:[NSString stringWithFormat:@"%i",currentPlayingIndex]] objectForKey:@"sound_url"];
//here I get mp3 file from http url via NSRequest in NSData
NSData *soundData = [sharedAppSettingsController getSoundUrl:defaultDictionaryID uri:soundUrl];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithData:soundData error:&error];
audioPlayer.numberOfLoops = 0;
if (error) {
NSLog(@"Error: %@",[error description]);
}
else {
audioPlayer.delegate = self;
[audioPlayer play];
}
}
一切正常,除了一些声音的第一出戏精。应用程序冻结约2秒钟,并播放声音。第二和其他声音播放正好在点击声音按钮后正常工作。
我想知道为什么在应用程序启动时第一次播放时会停留约2秒?
如果你认为我拿错解,请告诉我正确的选择。
问候
谢谢,我会试试这个... – 2010-09-23 08:38:26