我有一个钢琴应用程序。它工作正常,有一点错误。如果我同时快速弹奏几个键,声音会消失几秒钟,并在控制台中收到以下消息
AudioQueueStart 发布消息以杀死 mediaserverd
以下是相关代码:
-(IBAction)playNoteFromKeyTouch:(id) sender{
[NSThread detachNewThreadSelector:@selector(playNote:) toTarget:self withObject:[NSString stringWithFormat:@"Piano.mf.%@",[sender currentTitle]]];
}
-(void)playNote:(NSString *) note{
NSError *err;
NSString *path = [[NSBundle mainBundle] pathForResource:note ofType:@"aiff"];
AVAudioPlayer *p = [[AVAudioPlayer alloc ] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&err];
p.delegate = self;
if (err) {
NSLog(@"%@", err);
}else{
[p prepareToPlay];
[p play];
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[player release];
}
我用 Instruments 测试过,没有任何内存泄漏。如果有人能想到避免这个错误,我们将不胜感激。