不要播放声音
我把我的项目转换成ARC。 Xcode在例子的代码中改变了这一行
soundFileURLRef = (CFURLRef) [tapSound retain];
至
soundFileURLRef = (__bridge CFURLRef) tapSound;
但没有声音播放。 怎么了?
- (IBAction)buttonTapped:(UIButton *)sender { // Create the URL for the source audio file. The URLForResource:withExtension: method is new in iOS 4.0 NSURL *tapSound = [[NSBundle mainBundle] URLForResource: @"abide" withExtension: @"aif"]; CFURLRef soundFileURLRef; SystemSoundID soundFileObject; // Store the URL as a CFURLRef instance soundFileURLRef = (__bridge CFURLRef) tapSound; // I think this line is wrong // Create a system sound object representing the sound file. AudioServicesCreateSystemSoundID ( soundFileURLRef, &soundFileObject ); AudioServicesPlaySystemSound (soundFileObject); }
从逻辑上讲,你的代码看起来非常好。 我怀疑你实际上没有把abide.aif
和你的应用程序捆绑在一起,或者你不小心input了错误的名字(扩展名可能是aiff
?)。 您可以通过在[NSBundle URLForResource:withExtension:]
调用之后放置一个断点并检查tapSound
的内容来validation这tapSound
。
确保您已经将其包含在“复制捆绑资源”中的目标中:
另外,您应该知道,对AudioServicesCreateSystemSoundID
调用应该使用AudioServicesDisposeSystemSoundID
调用进行平衡。 你在这里不必要地分配内存,ARC不会为你处理这个问题。
这样做的理想方法是完成所有工作,在您的awakeFromNib
或viewDidLoad
方法中创build一个SystemSoundID
,将其存储为一个ivar,并只在您的buttonAudioServicesPlaySystemSound
方法中调用AudioServicesPlaySystemSound
。 在你的dealloc方法中,你应该调用AudioServicesDisposeSystemSoundID
。
让我知道事情的后续。