我有以下问题让我发疯。在用户按下按钮后,我的应用程序必须每秒播放指定次数(例如 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
请帮我!
非常感谢您提前。
——卡罗