objective-c - 如果我启用了 ARC,是否需要释放 SystemSoundID?

标签 objective-c ios xcode avfoundation audiotoolbox

问题在标题中:“如果我打开了 ARC,我需要释放 SystemSoundID 吗?”这是我的代码:

NSURL *pathURL = [NSURL fileURLWithPath:path];
SystemSoundID soundid;
AudioServicesCreateSystemSoundID((__bridge_retained CFURLRef)pathURL, &soundid);
AudioServicesPlaySystemSound(soundid);

如果是这样,我什么时候发布它? (我没有 dealloc 方法,因为我使用的是静态方法并且无法更改)

另外,这是目前播放音效的最佳方式吗?我听说这个框架现在已被弃用。

谢谢!

最佳答案

是的,您确实需要释放它。 ARC 只关心 Obj-C 对象,而 SystemSoundID 不是 obj-c 对象。在某些时候,您确实需要对 SystemSoundID 值调用 AudioServicesDisposeSystemSoundID()。您可以使用系统声音完成例程(使用 AudioServicesAddSystemSoundCompletion())来完成此操作。

至于 sergio 所说的内容,您正在泄漏 pathURL 对象。您使用了__bridge_retained,它将对象的所有权转移到CoreFoundation。您可能应该将其更改为 (__bridge CFURLRef)pathURL,这不会转移所有权。 AudioServices API 将根据需要保留该对象。

关于objective-c - 如果我启用了 ARC,是否需要释放 SystemSoundID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11253760/

相关文章:

iphone - 保存表格 View 的状态/保存表格 View 文本的状态

ios - UIView autoresizingMask 在设置不同的固定左右边距和灵活宽度时出现错误?

iOS 10 漏洞 : UICollectionView received layout attributes for a cell with an index path that does not exist

ios - 用于测试 iPhone 应用程序的 iPad mini

ios - UICollectionViewFlowLayout 的 estimatedItemSize 中断滚动?

iOS 命令行构建失败(Jenkins、Xcode 6.1、Storyboards)

objective-c - 在 web 服务 WSDL 中使用点符号导致 sudzc 编译错误

objective-c - NSOperation子类发布使Instruments崩溃

ios - 无法在界面生成器中编辑工具栏的属性

objective-c - Target-Action 设计模式是否在 ARC 下变成了坏习惯?