Problems with AVAudioPlayer

I am new to iPhone-Development.

I want to play sound while listening to UIButton. 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 application, I get the following error:

Undefined characters for i386 architecture: "_OBJC_CLASS _ $ _ AVAudioPlayer" referenced: objc-class-ref in JahrenzeitenViewController.o ld: character not found for architecture i386 collect2: ld returned 1 exit status

I also tried changing the line

 audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:&error]; 

to

 audioPlayer = [audioPlayer initWithContentsOfURL:audioFileURL error:&error]; 

Then I do not get the error above, but it still does not play, and I get DebugMessage from my NSLog in "playSound" -Method:

Error playing sound. (Null)

I hope you help me. Thanks in advance:)

+7
source share
4 answers

You need to add the AVFoundation framework to your project. (right-click on your primary goal, then add the existing infrastructure).

+30
source

To add to other answers.

You should always look at the documentation for the Ie class AVAudioPlayer . That you are not familiar with when you intend to use it. There, at the top of the Class Reference documentation for the class, there is a table showing which structure the class belongs to:

enter image description here

Once you know this, add the framework to the project. And also import it into the header file (.h), as others have pointed out,

+6
source

Thank you, it worked. I did not know that I needed to add Framework: S

I used the following instructions:

  • In the project navigator, select your project
  • Choose a target
  • Select the Phase Assembly tab
  • Open the 'Link Binaries With Libraries' Extender
  • Click the "+" button
  • Choose a framework
+4
source

Pls add an AVFoundation framework to your project. And I think you should add <AVFoundation/AVFoundation.h> along with <AudioToolbox/AudioToolbox.h>

0
source

All Articles