不播放声音 [英] Don't play sound

查看:195
本文介绍了不播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的项目ARC。 X $ C $下在code改变这条线从例子

  soundFileURLRef =(CFURLRef)tapSound保留]

  soundFileURLRef =(__bridge CFURLRef)tapSound;

但没有播放声音。怎么了?

   - (IBAction为)buttonTapped:(UIButton的*){发件人
        //创建源音频文件的URL。该URLForResource:withExtension:方法iOS 4.0的新
        NSURL * tapSound = [[一个NSBundle mainBundle] URLForResource:@恪守withExtension:@AIF];        CFURLRef soundFileURLRef;
        SystemSoundID soundFileObject;        //保存URL作为CFURLRef实例
        soundFileURLRef =(__bridge CFURLRef)tapSound; //我觉得这条线是错误的        //创建一个系统声音对象重新presenting声音文件。
        AudioServicesCreateSystemSoundID(                                          soundFileURLRef,
                                          &安培; soundFileObject
                                          );        AudioServicesPlaySystemSound(soundFileObject);
    }


解决方案

从逻辑上讲,你的code看起来完全正常。我怀疑你有没有真正捆绑 abide.aif 您的应用程序,或者你不小心打错(可扩展性 AIFF ?)。 电话和检查 tapSound [:withExtension一个NSBundle URLForResource]经过验证这一点>。

要确保你实际上它包含为目标的复制包资源

此外,你应该知道,调用 AudioServicesCreateSystemSoundID 应该用 AudioServicesDisposeSystemSoundID 调用来平衡。你是在不必要在这里分配内存和ARC不会照顾这对你。

要做到这一点,理想的办法是做好各项工作,以创建一个 SystemSoundID awakeFromNib viewDidLoad中方法,将其存储为伊娃,只调用 AudioServicesPlaySystemSound 在你的按钮水龙头方法。在你的dealloc方法,你应该叫 AudioServicesDisposeSystemSoundID

让我知道如何去。

I converted my project to ARC. Xcode changed this line in code from example

soundFileURLRef = (CFURLRef) [tapSound retain];

to

soundFileURLRef = (__bridge CFURLRef) tapSound;

but no sound plays. What's wrong?

- (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);
    }

解决方案

Logically, your code looks completely fine. I suspect that you have not actually bundled abide.aif with your application, or you have accidentally typed the name wrong (could the extension be aiff?). You can verify this by putting a breakpoint after the [NSBundle URLForResource:withExtension:] call, and checking the contents of tapSound.

Be sure that you have actually included it for your target in the "copy bundle resources":

In addition, you should be aware that calls to AudioServicesCreateSystemSoundID should be balanced with a AudioServicesDisposeSystemSoundID call. You are allocating memory needlessly here, and ARC will not take care of this for you.

The ideal way to do this would be to do all the work to create a SystemSoundID in your awakeFromNib or viewDidLoad method, store it as an ivar, and only call AudioServicesPlaySystemSound in your button tap method. In your dealloc method, you should call AudioServicesDisposeSystemSoundID.

Let me know how it goes.

这篇关于不播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆