我有AVAudioPlayer的問題。 我想在播放聲音之前給UICollectionView的一個單元着色,但是使用這段代碼我無法做到這一點。在AVAudioPlayer播放前做點什麼
我認爲改變背景的操作比播放聲音的指令更慢,因此單元不會改變背景顏色。
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];
}
我的問題是播放指令之前的操作。播放指令後的操作正確。 – zp26