title: AudioServicesAddSystemSoundCompletion under ARC using __bridge
body: <p>I hope the title is not too misleading... :)</p>
  
  <p>I play a system sound and add the SoundCompletion-Callback to it like so:</p>
  
  <pre><code>AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
  </code></pre>
  
  <p>Whereas «self» is a simple NSObject</p>
  
  <p>In the completion callback I try to call the playing routine again:</p>
  
  <p>I had to add the __bridge_transfer and the __bridge_retained to the casts, otherwise I get errors, crashes, or other unexpected behaviour.</p>
  
  <p>But the whole thing doesn't work despite all that.</p>
  
  <p>I store the sounds to play in an NSMutableArray, grab the first entry of the array and play it, add the sound completion and hope stuff happens.
  But - with all that retained-transfer stuff, the NSMutableArray is empty on the second call...</p>
  
  <p>Here's the code:</p>
  
  <pre><code>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] &gt; 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, &amp;soundID);
          AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
          AudioServicesPlaySystemSound (soundID);
      }
  }
  </code></pre>
  
  <p>[EDIT] - ANSWERING MY OWN QUESTION:</p>
  
  <p>Always nice to find it out myself :)</p>
  
  <p>Turns out, I was almost there.</p>
  
  <p>The call to set up the callback is as follows:</p>
  
  <pre><code>AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, completionCallback, (__bridge_retained void *)self);
  </code></pre>
  
  <p>Then, in the callback-function, I do this:</p>
  
  <pre><code>myClass *theClass = (__bridge myClass *)myself;
      CFRelease(myself);
      [theClass playNextSound]; // The routine that plays the sounds
  </code></pre>
  
  <p>And it works...</p>