当按下按钮后每秒必须播放短声音时如何释放 AVAudioPlayer 对象

发布于 2024-10-10 00:34:30 字数 1233 浏览 4 评论 0原文

我有以下问题让我抓狂。 在用户按下按钮后,我的应用程序必须每秒播放指定次数的滴答声(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-10-17 00:34:30

我发现了更多系统声音服务比 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.

哆啦不做梦 2024-10-17 00:34:30

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

self.player_tic = [[[AVAudioPlayer alloc] initWithContentsOfURL:... error:nil] autorelease]

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.

self.player_tic = [[[AVAudioPlayer alloc] initWithContentsOfURL:... error:nil] autorelease]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文