AVAudioPlayer 的问题
我是 iPhone 开发新手。
我想在点击 UIButton 时播放声音。我尝试了以下操作:
ViewController.h:
@interface JahrenzeitenViewController : UIViewController <AVAudioPlayerDelegate> {
UIButton *button_PlaySound;
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) IBOutlet UIButton *button_PlaySound;
ViewController.m
- (IBAction)playSound {
//[audioPlayer release];
//audioPlayer = nil;
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer play];
if (audioPlayer == nil)
NSLog(@"Error playing sound. %@", [error description]);
else
[audioPlayer play];
如果我尝试运行该应用程序,则会收到以下错误:
架构 i386 的未定义符号: “_OBJC_CLASS_$_AVAudioPlayer”,引用自: JahrenzeitenViewController.o 中的 objc-class-ref ld:未找到架构 i386 的符号 collect2:ld返回1退出状态
我还尝试将行更改
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
为
audioPlayer = [audioPlayer initWithContentsOfURL:audioFileURL error:&error];
Then 我没有收到上面的错误,但它仍然没有播放,并且我在“playSound”方法中从 NSLog 中获取了 DebugMessage:
播放声音时出错。 (空)
我希望你能帮助我。先感谢您 :)
I'm new to the iPhone-Development.
I want to play a Sound when an UIButton is tapped. I tried the following:
ViewController.h:
@interface JahrenzeitenViewController : UIViewController <AVAudioPlayerDelegate> {
UIButton *button_PlaySound;
AVAudioPlayer *audioPlayer;
}
@property (nonatomic, retain) IBOutlet UIButton *button_PlaySound;
ViewController.m
- (IBAction)playSound {
//[audioPlayer release];
//audioPlayer = nil;
NSString *audioFilePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"wav"];
NSURL *audioFileURL = [NSURL fileURLWithPath:audioFilePath];
NSError *error = nil;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
[audioPlayer setDelegate:self];
[audioPlayer prepareToPlay];
[audioPlayer play];
if (audioPlayer == nil)
NSLog(@"Error playing sound. %@", [error description]);
else
[audioPlayer play];
If I try to run the App, I get the following error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_AVAudioPlayer", referenced from:
objc-class-ref in JahrenzeitenViewController.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
I also tried to change the Line
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error];
to
audioPlayer = [audioPlayer initWithContentsOfURL:audioFileURL error:&error];
Then I don't get the error above, but its still not playing and i get the DebugMessage from my NSLog in the "playSound"-Method:
Error playing sound. (NULL)
I hope you can help me. Thank you in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将 AVFoundation 框架添加到您的项目中。 (右键单击您的主要目标,然后添加现有框架)。
You need to add the
AVFoundation
framework to your project. (right-click on your main target then add existing framework).添加到其他答案。
您应该始终查看类“Ie AVAudioPlayer”的文档。当你打算使用它时你不熟悉它。类的文档(类参考)顶部有一个表格,显示该类属于哪个框架:
一旦您知道这一点,请将框架添加到项目中。并将其导入到头文件(.h)中,正如其他人指出的那样
To add to the other answers.
You should always look at the documentation of a class 'I.e AVAudioPlayer. That you are unfamiliar with when you intend to use it. There at the top of the documentation ( Class Reference) for the the Class, is a table showing what framework the class belongs to:
Once you know this add the framework to the project. And also import it into the header (.h) file as others have pointed out
谢谢你,它成功了。我不知道我必须添加框架:S
我使用了以下说明:
thank you, it worked. I didn't know that I have to add the Framework :S
I used the following instructions:
请将 AVFoundation 框架添加到您的项目中。我认为您应该添加
以及Pls add the AVFoundation framework to your project. And i think you should add
<AVFoundation/AVFoundation.h>
along with<AudioToolbox/AudioToolbox.h>