开发者

Problems with AVAudioPlayer

开发者 https://www.devze.com 2023-02-19 03:54 出处:网络
I\'m new to the iPhone-Development. I want to play a Sound when an UIButton is tapped. I tried the following:

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


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


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:

Problems with AVAudioPlayer

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消