当我在Xcode中运行我的应用程序时,当我按下附有声音的按钮时它会崩溃

时间:2012-08-10 13:27:02

标签: objective-c ios xcode xcode4.4

所以正在制作这个应用程序,如果你按一个按钮,声音播放,但当我按下按钮时,应用程序崩溃。这是我的代码

在.h文件中

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController : UIViewController {

}

-(IBAction)buttonPressedWithSound:(id)sender;

@end

没有错,但是在.m文件中

-(IBAction)buttonPressedWithSound:(id)sender {

int randomSoundNumber = arc4random() % 4; //random number from 0 to 3

NSLog(@"random NR = %i", randomSoundNumber);

NSString *effectTitle;

switch (randomSoundNumber) {
    case 0:
        effectTitle = @"Come at me BRO!";
        break;
    case 1:
        effectTitle = @"sound2";
        break;
    case 2:
        effectTitle = @"sound3";
        break;
    case 3:
        effectTitle = @"sound4";
        break;

    default:
        break;
}

SystemSoundID soundID;

NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:@"caf"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];

AudioServicesCreateSystemSoundID ((CFURLRef)CFBridgingRetain(soundUrl), &soundID);
AudioServicesPlaySystemSound(soundID);
}

@end

这一行

 NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
好吧,它说它有问题,这个

 2012-08-10 10:12:52.419 Sound +[1177:907] random NR = 1(lldb)

 NSURL *soundUrl = [NSURL fileURLWithPath:soundPath]; 

有一个绿色突出显示并在其旁边说“线程1:断点1.1”。

我如何修复它并且可以播放声音而不会崩溃

2 个答案:

答案 0 :(得分:1)

您的代码记录了您的要求,然后点击您在代码中设置的断点。点击“继续”按钮。

答案 1 :(得分:1)

您应该使用NSSound类:

NSSound *sound = [[[NSSound alloc] initWithContentsOfFile: soundPath byReference: NO] autorelease];

[sound play];