当按下按钮后每秒必须播放短声音时如何释放 AVAudioPlayer 对象
我有以下问题让我抓狂。 在用户按下按钮后,我的应用程序必须每秒播放指定次数的滴答声(例如 5 次)。
我用的是这个:
<代码>
for (int w=1; w<=5; w++) {
[NSThread detachNewThreadSelector:@selector(tic) toTarget:self withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
其中:
<代码> -(无效)抽动{
NSAutoreleasePool *ticPool = [[NSAutoreleasePool alloc] init];
player_tic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource:@"tick" ofType:@"aif"]] 错误:nil];
[player_tic setDelegate:self];
[player_tic 播放];
[ticPool排水];
}
和:
<代码> - (void)audioPlayerDidFinishPlaying: (AVAudioPlayer *)player_tic 成功:(BOOL)flag {
NSLog(@"音频播放完毕。");
[player_tic 发布];
}
一切
似乎都有效。我的问题是对象最后一起释放。
我需要在每个播放声音后,必须立即释放相关的“player_tic”。
其他信息:
在 .m:
<代码> @合成player_tic;
在 .h 中:
<代码> @interface myController : UIViewController {
...
AVAudioPlayer *player_tic;
}
@property(非原子,保留)AVAudioPlayer *player_tic;
最重要的是,我在编译时收到此警告:
<代码> “player_tic”的本地声明隐藏实例变量
请帮助我!
预先非常感谢您。
——卡洛
I have the following problem making me crazy.
My app has to play a tick sound every second for a specified number of times (e.g. 5) after the user has pressed a button.
I used this:
for (int w=1; w<=5; w++) {
[NSThread detachNewThreadSelector:@selector(tic) toTarget:self withObject:nil];
[NSThread sleepForTimeInterval:1.0];
}
where:
- (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];
}
and:
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)player_tic successfully:(BOOL)flag {
NSLog(@"Audio finished playing.");
[player_tic release];
}
All seems to work. My problem is that the object are released together at the end.
I need that after every played sound the related 'player_tic' has to be released immediately.
Other Info:
In .m:
@synthesize player_tic;
In .h:
@interface myController : UIViewController {
...
AVAudioPlayer *player_tic;
}
@property (nonatomic, retain) AVAudioPlayer *player_tic;
On top of all, I have this warning in compilation:
local declaration of 'player_tic' hides instance variable
Please help me!
Thank you very much in advance.
--Carlo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了更多系统声音服务比 AVAudioPlayer 反应灵敏。
它还解决了您的释放问题,这是由控制器的保留属性引起的。有关保留/释放的更多信息,请阅读有关内存管理的参考手册。
I found System Sound Services more responsive than AVAudioPlayer.
It also solves your release problem, which is caused by the retain property of your controller. Read the reference manual on memory management for further information on retain/release.
我认为您想在设置该变量时使用 self.player_tic,并且我相信您也想自动释放该实例。我也不确定是否有必要设置一个单独的 NSAutoreleasePool 。
I think you want to use self.player_tic when setting that variable, and i believe you also want to autorelease that instance. I'm not sure that setting up a separate NSAutoreleasePool is necessary either.