3

When calling AudioServicesPlaySystemSound, it plays well the previous created SystemSound. But when i call AudioServicesDisposeSystemSoundID directly afterwards, the sound is not played. It seems as the Sound-Resource is disposed before it would have been played.

But on this Stackoverflow-Question the people say, that it is necessary to dispose the System-Sound to avoid memory leaks.

How is the correct way for both methods? Shall i call the Dispose-Method with a timer some seconds later? Or create a singleton which creates the Sound-Resource once at the first call of the Play-Method and dispose the Resource at dealloc of the singleton?

- (void) playSound {
    NSURL *url   = [[NSBundle mainBundle] URLForResource: @"SentMessage"
                                           withExtension: @"wav"];

    SystemSoundID mBeep;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)url,  &mBeep );

    AudioServicesPlaySystemSound(mBeep);
    AudioServicesDisposeSystemSoundID(mBeep);  
}
Community
  • 1
  • 1
itinance
  • 11,711
  • 7
  • 58
  • 98

1 Answers1

4

You can use AudioServicesAddSystemSoundCompletion to register a callback to be called when your sound finishes playing. You could dispose of the system sound there.

Or in your class's dealloc.

Rhythmic Fistman
  • 34,352
  • 5
  • 87
  • 159