我对 AVAudioPlayer 有疑问。我想在声音播放之前为 UICollectionView 的单元格着色,但是使用此代码我无法执行此操作。
我想改变背景的操作比播放声音的指令慢,因此单元格不会改变背景颜色。
int randomNumber = (arc4random() % [soundArray count]);
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[soundArray objectAtIndex:randomNumber] error:&error];
self.audioPlayer.delegate = self;
if(!self.audioPlayer){
NSLog(@"Error creating player: %@", error);
}
else{
CGColorRef colorRef = [[cellArray objectAtIndex:randomNumber] backgroundColor];
[[cellArray objectAtIndex:randomNumber] setBackgroundColor:(__bridge CGColorRef)([UIColor blackColor])];
[self.audioPlayer play];
sleep([self.audioPlayer duration]);
[[cellArray objectAtIndex:randomNumber] setBackgroundColor:colorRef];
}