0
votes

I'm trying to play a mp3 file in objective c with Xcode for the iPhone.

In viewDidLoad:

 NSURL *mySoundURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/mySound.mp3", [[NSBundle mainBundle] resourcePath]]];

NSError *myError;
mySound = [[AVAudioPlayer alloc] fileURLWithPath:heartBeatURL error:&myError];
[mySound play];

I found a suggestion here: Problem while playing sound using AVAudioPlayer?

but it did not work for me, it only generated more issues.

When the program launches I get this in output and the program crashes:

Undefined symbols for architecture i386: "_OBJC_CLASS_$_AVAudioPlayer", referenced from: objc-class-ref in SecondViewController.o ld: symbol(s) not found for architecture i386 collect2: ld returned 1 exit status

What am I doing wrong here?

2
have you imported the AVFoundation framework in you ViewController header?Bazinga

2 Answers

1
votes

It looks to me like you haven't linked the AVFoundation framework into your app.

Assuming recent enough xcode:

  1. Select your project in the Project Navigator on the left
  2. Select your Target
  3. Select Build Phases
  4. Add AVFoundation.framework to the Link Binary With Libraries phase

Here's some working AVAudioPlayer code for comparison:

NSURL *mySoundURL = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"BadTouch" ofType:@"mp3"]];
NSError *myError;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundURL error:&myError];
[self.player play];
1
votes

Add AVFoundation.framework to your Projects Target Link Binary With Libraries

Then import in your .h:

#import <AVFoundation/AVFoundation.h>

@interface ViewController : UIViewController <AVAudioPlayerDelegate> {

    AVAudioPlayer *player;
    }
@property (strong,nonatomic) AVAudioPlayer *player;

@end

in your .m:

    @synthesize player;


    NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
    resourcePath = [resourcePath stringByAppendingString:@"/mySound.mp3"];
    NSLog(@"Path to play: %@", resourcePath);
    NSError* err;

    //Initialize our player pointing to the path to our resource
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:
              [NSURL fileURLWithPath:resourcePath] error:&err];

    if( err ){
        //bail!
        NSLog(@"Failed with reason: %@", [err localizedDescription]);
    }
    else{
        //set our delegate and begin playback
        player.delegate = self;
        [player play];

    }