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 trial

iOS

John Randak
John Randak
2,552 Points

Problem using AVAudio Player

So I read over this discussion:

https://teamtreehouse.com/forum/creating-an-array-with-mp3-sound-files

But I'm still having trouble getting my AVAudio player to work. Here is the code I have:

- (void) playClip {
NSString *mp3File = [[NSBundle mainBundle] pathForResource:@"inFavor" ofType:@"mp3"];
AVAudioPlayer *avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:mp3File] error:NULL];
[avPlayer play];
}

I'm using this method inside IBAction button pressed action so that when the button is pressed:

- (IBAction) clickHere {
[self playClip]; 
}

I've imported the AV foundation at the top. not sure what I'm missing.

thanks!

John Randak
John Randak
2,552 Points

Really what I'm hoping to do is create an Array of mp3 files. If anyone has done this and would be willing to share their code with me I would be really grateful!!

1 Answer

Stefan Bjornsson
Stefan Bjornsson
3,362 Points

Don´t declare the code for the sound file in a specific method rather declare it in the ViewDidLoad method and then you can make a playClip method which you can paste the following code into: AudioServicesPlaySystemSound(soundEffect); Then you are passing what you declare in ViewDidLoad method into PlayClip method and when the button is pressed you call the playClip method. Hope this helps.