几次调用后不调用AudioServicesAddSystemSoundCompletion回调方法 [英] AudioServicesAddSystemSoundCompletion callback-method is not called after a few calls

查看:425
本文介绍了几次调用后不调用AudioServicesAddSystemSoundCompletion回调方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用AudioServicesAddSystemSoundCompletion来检测声音何时完成,然后触发其他一些操作。
出于某种原因,我得到以下行为,它适用于前8到12个声音(至少是我测试过的声音),然后不再调用为AudioServicesAddSystemSoundCompletion定义的回调。

I'm using AudioServicesAddSystemSoundCompletion in my app to detect when sound has finished and then trigger some other action. For some reason I am getting the following behavior, it works for the first 8 to 12 sounds (that's at least what I tested) and then the callback defined for AudioServicesAddSystemSoundCompletion is not being called anymore.

这是我创建声音的代码:

Here is my code to create the sound:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:[[soundFileName componentsSeparatedByString:@"."]objectAtIndex:0]ofType:@"wav"];
Log2(@"soundFileName: %@", soundFileName);
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(soundURL, &sound);
AudioServicesAddSystemSoundCompletion(sound, nil, nil, playSoundFinished, (void*) self);

播放声音:

AudioServicesPlaySystemSound(sound);

并在声音结束时做一些事情:

and to do some stuff when the sound finished playing:

void playSoundFinished (SystemSoundID sound, void *data) {
    pf //Typedef for PRETTY_FUNCTION log

    AudioServicesRemoveSystemSoundCompletion(sound);
    [AppState sharedInstance].b_touchesFREE = TRUE;

    if([[AppState sharedInstance].ma_cardsToCheck count] >= 2)
{        
        [[AppState sharedInstance].vc_spiel pairFound];
}   

    if (((Card*)data).b_isTurnedFaceUp) {
        [AppState sharedInstance].i_cardsTurnedFaceUp --;
    }

    [(Card*)data release];
}

有没有人知道为什么它在前几次工作然后停止工作?

Has anyone of you any idea why it works the first few times and then stops working?

Thx提前。
Maverick1st

Thx in advance. Maverick1st

*****编辑*****
我刚刚发现,当我尝试播放声音时会发生这种情况第二次。
可能是,我忘了把它放到某处?
但我一直认为AudioServicesRemoveSystemSoundCompletion处理内存管理。

***** Edit ***** I just found out, that this happens when i try to play the sound a second time. Could it be, that i forgot to release it somewhere? But I always thought AudioServicesRemoveSystemSoundCompletion handles the memory management.

*****再一次编辑*****
所以发布这个Stackoverflow让我对这个问题有了更深入的思考,现在我得到了解决方案(至少我认为我得到了它;))。
不幸的是,我无法在接下来的7.5小时内为自己回答这个问题所以我必须编辑这个问题。

***** One more edit ***** So posting this on Stackoverflow made me think a bit deeper about the problem and i got the solution now (at least i think i got it ;)). Unfortunately i cannot answer the question for myself for the next 7.5 hours so i have to edit the question.

只是为了让你更好地理解我的问题。

Just for you to better understand my problem.

我正在编写一个记忆游戏,每张卡片都是一个包含正面和背面图像以及它转身时播放声音的类。
因为我只是在创建卡时初始化声音我不确定每次声音结束时我是否应该调用AudioServicesRemoveSystemSoundCompletion。

I'm programming a memory game and every Card is a Class containing its image for front and back and the sound it plays when its turned around. since i only initialize the sound on creation of the card i was not sure if I should call AudioServicesRemoveSystemSoundCompletion every time the sound ends.

所以我只是试了一下没有AudioServicesRemoveSystemSoundCompletion它现在可以工作。

So i just tried it without AudioServicesRemoveSystemSoundCompletion and it works now.

我现在唯一不确定的是,这是否会导致内存泄漏或类似的事情。
但是现在它工作正常。

Only thing i am not sure about now is if this could lead to a memory leak or something like that. But for now it works fine.

如果有人能告诉我这是否可以用于内存使用我会真的发生。 :)

If someone could tell me if this is ok regarding the memory use i'd be really happe. :)

祝你好运。
Maverick1st

Best regards. Maverick1st

推荐答案

如果您在应用的生命周期内只创建一次声音(并设置声音完成),从内存管理的角度来看应该没问题。它始终是你创造的 - 你释放。

If you create the sound (and set sound completion) only once during the lifetime of the app, it should be ok from memory management standpoint. It is always you create - you release.

然而,你应该致电

AudioServicesRemoveSystemSoundCompletion(sound);
AudioServicesDisposeSystemSoundID(sound);

当你不再需要声音时(最有可能是你所在对象的dealloc方法)创建(或保持引用)声音。不要改变这两个的顺序,否则你会有内存泄漏。

when you don't need the sound anymore (most probably in the dealloc method of the object where you created (or keep reference to) the sound. Do not change the order of these two, otherways you have a memory leak.

也许你会发现 AppSoundEngine 很有用。使用SystemSoundID和相关C函数的包装器很简单。

Maybe you would find AppSoundEngine useful. It is simple to use wrapper for SystemSoundID and associated C functions.

这篇关于几次调用后不调用AudioServicesAddSystemSoundCompletion回调方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆