It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center。
7年前关闭。
我将项目转换为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
与您的应用程序 bundle 在一起,或者您不小心键入了错误的名称(扩展名为aiff
吗?)。您可以通过在[NSBundle URLForResource:withExtension:]
调用之后放置一个断点并检查tapSound
的内容来验证这一点。
确保您已将其实际包含在“复制 bundle 包资源”中作为目标:
另外,您应该意识到对AudioServicesCreateSystemSoundID
的调用应与AudioServicesDisposeSystemSoundID
调用保持平衡。您在这里不必要地分配内存,ARC不会为您处理此事。
理想的方法是完成所有工作,以便在SystemSoundID
或awakeFromNib
方法中创建viewDidLoad
,将其存储为ivar,然后仅在按钮tap方法中调用AudioServicesPlaySystemSound
。在您的dealloc方法中,应该调用AudioServicesDisposeSystemSoundID
。
让我知道事情的后续。