我有以下问题使我发疯。
用户按下按钮后,我的应用必须每秒播放一次滴答声,持续指定次数(例如5次)。
我用这个:
for (int w=1; w<=5; w++) {
[NSThread detachNewThreadSelector:@selector(tic) toTarget:self withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
哪里:
- (void)tic {
NSAutoreleasePool *ticPool = [[NSAutoreleasePool alloc] init];
player_tic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"tick" ofType:@"aif"]] error:nil];
[player_tic setDelegate:self];
[player_tic play];
[ticPool drain];
}
和:
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)player_tic successfully:(BOOL)flag {
NSLog(@"Audio finished playing.");
[player_tic release];
}
一切似乎正常。我的问题是该对象最后一起释放。
我需要在每播放一次声音后,必须立即释放相关的“player_tic”。
其他资讯:
在.m中:
@synthesize player_tic;
在.h中:
@interface myController : UIViewController {
...
AVAudioPlayer *player_tic;
}
@property (nonatomic, retain) AVAudioPlayer *player_tic;
最重要的是,我在编译中有以下警告:
local declaration of 'player_tic' hides instance variable
请帮我!
提前非常感谢您。
-卡洛
最佳答案
我发现System Sound Services比AVAudioPlayer更敏感。
它还解决了由 Controller 的keep属性引起的释放问题。阅读有关内存管理的引用手册,以获取有关保留/释放的更多信息。
关于ios4 - 如何在按下按钮后每秒必须播放简短声音的情况下释放AVAudioPlayer对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4569495/