Problems with AVAudioPlayer

不羁岁月 提交于 2019-12-21 10:45:30

问题


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 :)


回答1:


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




回答2:


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




回答3:


thank you, it worked. I didn't know that I have to add the Framework :S

I used the following instructions:

  1. In the project navigator, select your project
  2. Select your target
  3. Select the 'Build Phases' tab
  4. Open 'Link Binaries With Libraries' expander
  5. Click the '+' button
  6. Select your framework



回答4:


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



来源:https://stackoverflow.com/questions/5376437/problems-with-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!