Reputation: 349
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?
Upvotes: 0
Views: 805
Reputation: 2466
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];
}
Upvotes: 1
Reputation: 343
It looks to me like you haven't linked the AVFoundation framework into your app.
Assuming recent enough xcode:
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];
Upvotes: 1