使用__bridge在ARC下的AudioServicesAddSystemSoundCompletion

Swi*_*ude 5 iphone audiotoolbox ios automatic-ref-counting

我希望标题不会太误导...... :)

我播放系统声音并将SoundCompletion-Callback添加到它中:

AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
Run Code Online (Sandbox Code Playgroud)

而«self»是一个简单的NSObject

在完成回调中,我尝试再次调用播放例程:

我必须将__bridge_transfer和__bridge_retained添加到强制转换,否则我会收到错误,崩溃或其他意外行为.

但尽管如此,整个事情仍无法奏效.

我存储声音在NSMutableArray中播放,抓取数组的第一个条目并播放它,添加声音完成并希望发生的事情.但是 - 使用所有保留传输的东西,NSMutableArray在第二次调用时为空...

这是代码:

static void completionCallback (SystemSoundID  mySSID, void *myself) {

    NSLog(@"Audio callback");

    AudioServicesRemoveSystemSoundCompletion (mySSID);
    AudioServicesDisposeSystemSoundID(mySSID);

    [(__bridge_transfer Speaker *)myself speakCharacter];

    CFRelease(myself); // I heard I need this?

}

-(void)speakCharacter{

    if([sounds count] > 0){

        NSString *soundToPlay = [sounds objectAtIndex:0];
        [sounds removeObjectAtIndex:0];
        NSLog(@"TxtToSpeak %@", soundToPlay);
        CFURLRef        soundFileURLRef;
        NSURL *path = [[NSBundle mainBundle] URLForResource:[soundToPlay uppercaseString] withExtension:@"aif"];
        soundFileURLRef = (__bridge CFURLRef)path;
        SystemSoundID soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
        AudioServicesPlaySystemSound (soundID);
    }
}
Run Code Online (Sandbox Code Playgroud)

[编辑] - 回答我自己的问题:

总是很高兴自己找到它:)

事实证明,我几乎就在那里.

设置回调的调用如下:

AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
Run Code Online (Sandbox Code Playgroud)

然后,在回调函数中,我这样做:

myClass *theClass = (__bridge myClass *)myself;
    CFRelease(myself);
    [theClass playNextSound]; // The routine that plays the sounds
Run Code Online (Sandbox Code Playgroud)

它有效......

Swi*_*ude 8

我无法回答我自己的问题,因为我对StackOverflow来说太快了 - 所以为了完成这个,我再次添加答案:)

事实证明,我几乎就在那里.

设置回调的调用如下:

AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
Run Code Online (Sandbox Code Playgroud)

然后,在回调函数中,我这样做:

myClass *theClass = (__bridge myClass *)myself;
CFRelease(myself);
[theClass playNextSound]; // The routine that plays the sounds
Run Code Online (Sandbox Code Playgroud)

它有效......