当我触摸特定按钮时我的应用程序崩溃
这是我的按钮的标题。
IBOutlet UIButton *buttonOneOne;
}
- (IBAction)buttonOneOne:(id)sender;
@property (nonatomic, retain) IBOutlet UIButton *buttonOneOne;
这就是我放入 .m 文件中的内容,
- (IBAction)buttonOneOne:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"95" ofType:@".wav"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[musicPlayer prepareToPlay];
[musicPlayer setVolume:.8];
[musicPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[musicPlayer setDelegate: self];
[musicPlayer play];
}
为什么它会崩溃?
Here is the header for my button.
IBOutlet UIButton *buttonOneOne;
}
- (IBAction)buttonOneOne:(id)sender;
@property (nonatomic, retain) IBOutlet UIButton *buttonOneOne;
This is what i put in my .m file for it
- (IBAction)buttonOneOne:(id)sender {
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"95" ofType:@".wav"];
NSError *activationError = nil;
NSError *audioPlayerInitError = nil;
[[AVAudioSession sharedInstance] setActive: YES error:&activationError];
NSURL *newURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *musicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:newURL error:&audioPlayerInitError];
[musicPlayer prepareToPlay];
[musicPlayer setVolume:.8];
[musicPlayer setNumberOfLoops:-1]; // -1 means play indefintely
[musicPlayer setDelegate: self];
[musicPlayer play];
}
why is it crashing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*** -[NSURL initFileURLWithPath:]: nil 字符串参数”
表示无法找到您引用的文件。指定文件扩展名时不应使用点。
更改
类型:@".wav"
到
ofType:@"wav"
NSBundle 类参考
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
means that the file you're referencing cannot be found.You should not use the dot when specifying the file extension.
Change
ofType:@".wav"
to
ofType:@"wav"
NSBundle Class Reference
您应该发布一些控制台输出(可能是堆栈跟踪),这会对我们有更多帮助。
我可以从这段代码中看到崩溃的可能原因:
您在为其添加
@property
后是否合成
您的按钮?您的 Xcode 项目中是否存在
95.wav
(您已导入它)?您正在使用 Interface Builder 吗?如果是这样,请检查您的 IB 项目中是否有旧的 IBOutlets 和 IBActions,因为一旦您重命名了函数或类似的东西,您必然会获得指向旧代码的界面元素。
You should post some console output (possibly a stack trace), it would help us a LOT more.
Possible causes of a crash I can see from this code:
Did you
synthesize
your button after decalring an@property
for it?Does
95.wav
exist in your Xcode Project (have you imported it)?Are you using Interface Builder? If so, check your IB project for old IBOutlets and IBActions, as once you rename a function or whatnot, you're bound to get interface elements pointing to old code.