当我触摸特定按钮时我的应用程序崩溃

发布于 2024-10-27 14:30:02 字数 886 浏览 2 评论 0原文

这是我的按钮的标题。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-11-03 14:30:02

由于未捕获的异常“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

枫以 2024-11-03 14:30:02

您应该发布一些控制台输出(可能是堆栈跟踪),这会对我们有更多帮助。

我可以从这段代码中看到崩溃的可能原因:

  1. 您在为其添加 @property 后是否合成您的按钮?

  2. 您的 Xcode 项目中是否存在 95.wav(您已导入它)?

  3. 您正在使用 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:

  1. Did you synthesize your button after decalring an @property for it?

  2. Does 95.wav exist in your Xcode Project (have you imported it)?

  3. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文