Objective-c – Problems with AVAudioPlayer

audioavaudioplayeriphoneobjective c

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 🙂

Best Answer

You need to add the AVFoundation framework to your project. (right-click on your main target then add existing framework).

Related Topic