Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 当我在Xcode中运行我的应用程序时,当我按下带有声音的按钮时,它会崩溃_Objective C_Ios_Xcode_Xcode4.4 - Fatal编程技术网

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

Objective c 当我在Xcode中运行我的应用程序时,当我按下带有声音的按钮时,它会崩溃,objective-c,ios,xcode,xcode4.4,Objective C,Ios,Xcode,Xcode4.4,所以我制作这个应用程序,如果你按下一个按钮,就会有声音播放,但当我按下按钮时,应用程序崩溃了。这是我的密码 在.h文件中 #import <UIKit/UIKit.h> #import <AudioToolbox/AudioToolbox.h> @interface ViewController : UIViewController { } -(IBAction)buttonPressedWithSound:(id)sender; @end 这条线在这里 NS

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

在.h文件中

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

@interface ViewController : UIViewController {

}

-(IBAction)buttonPressedWithSound:(id)sender;

@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]; 
有一个绿色的突出显示,并在旁边写着“Thread1:Breakpoint1.1”


我如何修复它并使它在不崩溃的情况下播放声音

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

您应该使用NSSound类:

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

[sound play];

还有谁知道更好的.m文件的代码,我不想让它播放一个以上的声音,每个按钮只播放一个声音打开你的声音路径。它可能无效。我是否要将soundpath更改为NSLog?不,在设置soundpath的值后,您需要将NSLog(@“%@”,soundpath)更改为NSLog。您的应用程序的主捆绑包中是否包含名为“sound2.caf”的文件?(点击您的目标,然后查看构建阶段选项卡的复制捆绑资源部分。)如果是,请发布控制台的堆栈跟踪。我确实设置了断点,但我对它们一无所知,尽管我只是在网站上看到过
NSSound *sound = [[[NSSound alloc] initWithContentsOfFile: soundPath byReference: NO] autorelease];

[sound play];