-1

我将我的项目转换为 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);
    }
4

1 回答 1

3

从逻辑上讲,您的代码看起来完全没问题。我怀疑您实际上并没有abide.aif与您的应用程序捆绑在一起,或者您不小心输入了错误的名称(扩展名可能是aiff?)。您可以通过在调用后放置断点[NSBundle URLForResource:withExtension:]并检查tapSound.

确保您已将其实际包含在“复制捆绑资源”中:

此外,您应该知道调用 toAudioServicesCreateSystemSoundID应该与AudioServicesDisposeSystemSoundID调用平衡。您在这里不必要地分配内存,ARC 不会为您处理这个问题。

理想的方法是完成所有工作以SystemSoundID在您的awakeFromNiborviewDidLoad方法中创建 a ,将其存储为 ivar,并且仅调用AudioServicesPlaySystemSound您的按钮点击方法。在您的 dealloc 方法中,您应该调用AudioServicesDisposeSystemSoundID.

让我知道事情的后续。

于 2012-09-26T12:17:39.433 回答