iOS 8:之后调用AudioServicesDisposeSystemSoundID时,AudioServicesPlaySystemSound没有播放声音

iOS 8: AudioServicesPlaySystemSound is not playing Sound when AudioServicesDisposeSystemSoundID is called afterwards

当调用AudioServicesPlaySystemSound时,它可以很好地播放之前创建的SystemSound。但是当我之后直接调用 AudioServicesDisposeSystemSoundID 时,声音没有播放。似乎声音资源在播放之前就被处理掉了。

但是在this Whosebug-Question上人们说,有必要处理System-Sound以避免内存泄漏。

两种方法的正确方法是怎样的?我应该在几秒钟后用计时器调用 Dispose-Method 吗?或者创建一个单例,在第一次调用 Play-Method 时创建一次声音资源,并在单例的 dealloc 处处理资源?

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

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

    AudioServicesPlaySystemSound(mBeep);
    AudioServicesDisposeSystemSoundID(mBeep);  
}

您可以使用 AudioServicesAddSystemSoundCompletion 注册一个回调,以便在您的声音播放完毕时调用。你可以在那里处理系统声音。

或者在你的 class 的 dealloc.