我正在尝试尽可能编写最简单的“按下按钮并听到声音”设置,我仍然设法得到一些神秘的错误。这是我的代码:
在.h文件中:
#import <AVFoundation/AVFoundation.h>
在.m文件中:
- (IBAction)beepButton:(UIButton *)sender {
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"buttonBeep"
ofType:@"mp3"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];
//[click release];
}
当我尝试编译我的应用程序时,我收到错误:
Undefined symbols for architecture armv7s:
"_OBJC_CLASS_$_AVAudioPlayer", referenced from:
objc-class-ref in MyFirstViewController.o
ld: symbol(s) not found for architecture armv7s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我做错了吗?任何帮助表示赞赏!
答案 0 :(得分:2)
这称为链接器错误。这一切都是在编译完所有内容后完成的。
问题在于符号_OBJC_CLASS_$_AVAudioPlayer
。你做的是忽略通过美元符号的部分,留下你AVAudioPlayer
。
由于您尝试使用AVAudioPlayer
类并导入<AVFoundation/AVFoundation.h>
,因此您需要意识到必须在项目中包含AVFoundation.framework
。
如果您查看AVAudioPlayer
的参考文档,您会看到文档说明它位于AVFoundation.framework
。这就是您知道需要将其添加到项目中的方式。
答案 1 :(得分:1)
您必须将AVFoundation.framework
添加到项目中,因为它包含AVAudioPlayer
类
答案 2 :(得分:1)
你添加了AVFoundation.framework吗?并确保您的.mp3文件出现在“复制包资源”
中