Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJohn Randak
2,552 PointspathForResource coming back nil??
Please help, I've been stuck on this for days now! I'm trying to get an AVAudioplayer to play something from an array I've created. Here is the code:
NSString* filename = [soundsList objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp3"];
//something is going wrong up here ^^^ because when I log "filename" it comes back as the mp3 I want to play "inFavor.mp3" but
//when I log "path" it comes back as null. the runtime error comes in when the audioplayer below tries to run something that is null.
NSLog(@"filename = %@", filename);
NSLog(@"path = %@", path);
NSError *error;
AVAudioPlayer * playClip=[[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:&error];
[playClip play];
when FILENAME is logged, it comes back as the file that I want but when I log the string PATH, it comes back as null. I made sure I selected "copy items to group folder" when I added the files. My guess is that for some reason the file is not showing up in the bundle... has anyone had experience with this?
John Randak
2,552 PointsPS—I should also mention that this code is under an IBAction in a button that gets pressed.
eirikvaa
18,015 PointsJust a tip: Try to break things up when you're debugging. Don't do it all in one line, it makes it harder to debug.
NSBundle *mainBundle = [NSBundle mainBundle]; NSString *path = [mainBundle pathForResource: filename ofType:@"mp3"];
This way you can check everything. If mainBundle doesn't come back nil (which it does if it couldn't create the object), then it must be that it can't find the file you are looking for in the directory you are checking. Did you copy it to the correct folder? Check that.
John Randak
2,552 PointsI'm pretty sure I have the file in the right location because now my "path" object is coming back as:
2014-02-07 15:30:11.875 sound[28389:70b] path = /Users/j-rah/Library/Application Support/iPhone Simulator/7.0.3/Applications/891A8EC3-48F9-4646-8FD0-92190B664F5B/sound.app/inFavor.mp3
also, the error is coming back as "nil."
I'm so confused! if path is referencing the right file, why isn't it playing?
1 Answer
eirikvaa
18,015 PointsI will recommend a tutorial by AppCoda that shows you how to set up an audioPlayer and play something. It uses the same framework as you (AVAudioPlayer). I'm currently designing my own app (so I don't have time to skim through the tutorial and point out what you're missing (if anyting)), but check it out!
John Randak
2,552 PointsI appreciate your feedback. I'll check out the tutorial you recommended. can you send me a link? :)
John
John Randak
2,552 PointsJohn Randak
2,552 Pointshere is my log message:
2014-02-07 10:50:31.594 sound[28071:70b] soundsList contains inFavor.mp3
2014-02-07 10:50:31.597 sound[28071:70b] soundsList contains bullshitMC.mp3
2014-02-07 10:50:31.597 sound[28071:70b] soundsList contains makeItGhetto.mp3
2014-02-07 10:50:31.598 sound[28071:70b] filename = inFavor.mp3
2014-02-07 10:50:31.599 sound[28071:70b] path = (null)
the first three are a printout of the array so that I know that my list "soundList" actually contains the 3 files.
the fourth line is the string FILENAME
the fifth (where things get screwed up) is the string PATH = (null)
:(