【发布时间】:2011-03-21 10:34:54
【问题描述】:
我是 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];
然后我没有收到上面的错误,但它仍然没有播放,我在“playSound”-方法中从我的 NSLog 中获取了 DebugMessage:
播放声音时出错。 (空)
我希望你能帮助我。提前谢谢你:)
【问题讨论】:
标签: iphone objective-c audio avaudioplayer