rd42
rd42

Reputation: 3594

Obj C just play mp3

I've seen this little piece of code in at least three tutorials,when I press the button connected to the action I get:

'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'

It says my path is null, which it is according to the console. Any idea on what I'm missing?

-(IBAction) pushStart
{
    NSString *arrowSoundPath = [[NSBundle mainBundle] pathForResource:@"Arrow.mp3" ofType:@"mp3"];
    NSLog(@"%@",arrowSoundPath);
    AVAudioPlayer *theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:arrowSoundPath] error:NULL];
    theAudio.delegate = self;
    [theAudio play];
}

Upvotes: 0

Views: 241

Answers (1)

Kyle
Kyle

Reputation: 1595

Remove the '.mp3' extension from the first parameter to -pathForResource:ofType:. It should be [[NSBundle mainBundle] pathForResource:@"Arrow" ofType:@"mp3"]. The way you have it now, -pathForResource:ofType: is looking for a file named 'Arrow.mp3.mp3', which doesn't exist, so it is returning nil, which is causing your error.

Upvotes: 5

Related Questions