0

我有以下问题让我发疯。在用户按下按钮后,我的应用程序必须每秒播放指定次数(例如 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

请帮我!
非常感谢您提前。

——卡罗

4

2 回答 2

0

我发现System Sound Services比 AVAudioPlayer 响应更快。

它还解决了您的发布问题,这是由您的控制器的保留属性引起的。阅读有关内存管理的参考手册以获取有关保留/释放的更多信息。

于 2010-12-31T10:36:16.880 回答
0

我认为您想在设置该变量时使用 self.player_tic,并且我相信您也想自动释放该实例。我也不确定是否需要设置单独的 NSAutoreleasePool。

self.player_tic = [[[AVAudioPlayer alloc] initWithContentsOfURL:... error:nil] autorelease]
于 2011-01-04T10:39:57.593 回答