我有类似应用程序的键盘,并且我将每个新的 AVAudioPlayer 添加到 NSMutableArray 以便我可以叠加声音。我的问题是,那个 delagate 函数
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
如果在短时间内有击键,则不会触发。例如,如果我以 0.5 秒或更长时间的间隔进行中风,一切正常。知道为什么吗?当他的声音播放完成时,我想释放内存并从数组中删除对象。
更新
。H
@interface .. <AVAudioPlayerDelegate>
{
NSMutableArray *soundObjects;
}
@property (nonatomic,strong) AVAudioPlayer *audioPlayer;
.m
-(void)playSound {
//
// play sound
//
_audioPlayer = [self loadWav:_soundID];
_audioPlayer.delegate = self;
[soundObjects addObject:_audioPlayer];
AVAudioPlayer *temp = [soundObjects objectAtIndex:soundIndex];
soundIndex = [soundObjects count];
[temp play];
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
#ifdef DEBUG_SOUND
NSLog(@"FLAG");
soundIndex--;
[soundObjects removeObjectAtIndex:soundIndex];
#endif
}
- (AVAudioPlayer *)loadWav:(NSString *)filename {
NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"mp3"];
NSError * error;
AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!player) {
#ifdef DEBUG_MODE
NSLog(@"Error loading %@: %@", url, error.localizedDescription);
#endif
} else {
[player prepareToPlay];
}
return player;
}