【发布时间】:2010-09-22 09:40:49
【问题描述】:
我正在使用 iOS SDK 中的 AVAudioPlayer 在 tableView 行中的每次单击时播放短声音。
我已经在每一行的按钮上手动创建了@selector,它启动了方法playSound:(id)receiver {}。从接收器我得到声音网址,所以我可以播放它。
这个方法看起来像这样:
- (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 秒的卡顿?
【问题讨论】:
标签: ios audio avaudioplayer