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 Build a Simple iPhone App (iOS7) Animating and Intercepting Events Adding Sound

The sound does not play using the project file included.

No sound is played using the sample project file provided on the Xcode 6 simulator on 10.10

5 Answers

If you just need to play a sound you can use this:

NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"crystal_ball" ofType:@"mp3"]; NSURL *soundURL = [NSURL fileURLWithPath:soundPath]; AudioServicesCreateSystemSoundID(CFBridgingRetain(soundURL), &soundEffect);

And remember to add the AudioToolbox framework.

Thanks for the reply.... I used the sample code that contained in the project files which contained the code you mentioned above. I don't have access to a iPhone that runs iOS 6 or 7, just using the simulator on Yosemite.

Regards,

Mohsin

Try changing the NSURL to use: URLForResource: withExtension: instead of fileURLWithPath:

- (void)viewDidLoad
{
    NSURL * soundURL = [[NSBundle mainBundle] URLForResource:@"soundEffect"
                                                withExtension:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef) soundURL, & soundEffect);
    AudioServicesPlaySystemSound(soundEffect);
}

Your code is probably correct (especially if you're simply running the sample project files provided by Treehouse). I encountered this issue and the problem turned out to be with some System Preferences on my Mac, not the code.

Go into System Preferences > Sound > Sound Effects and make sure "Play user interface sound effects" is checked. If it already is, uncheck and re-check.

H/T StackOverflow

The code is correct, I have the same problem with this. When I test the code on my actual phone, it works, but the simulator plays no sound.

I have tried all of the sound > sound effects options and none of it matters.

Did you try to close and re-open the simulator and/or Xcode after changing your preferences? And double-check that your speakers are on and volume is up?

Yes -

Within XCode, if i load iPhone 5s (7.1) the sound works.

Same file loading via XCode with iPhone 5s (8.0) the sound stops working.

IPhone 4s (8.0) no sound

iPhone 4s (7.1) sound works.

Hmm, well that's a different problem than the OP expressed. It sounds like there's been a change in iOS 8 that invalidates the code you're using to play the sound file. Or did you say that the sound does work on an actual iPhone running iOS 8? In that case it might be a bug with the way iOS 8 is implemented in the simulator.

The OP stated "No sound is played using the sample project file provided on the Xcode 6 simulator on 10.10"

XCode 6's default simulator is the 8.0 simulator, you have to install the 7.1 or 7.0.3 simulators under Preferences > Downloads (though I couldn't get 7.0 simulator to appear in the pulldown even when explicitly telling it to) if you want those for testing.

The sound does work when I demo it on my hardware 5s running iOS 8!

Do you know of a software "mixer" (similar to windows' audio mixer) that shows what input/output or if audio is functioning at a per-software level?

I have tried The Audio MIDI Setup as some people stated they could get it working if switching to 16bit and 44Hz instead of 24/48... but nope.

No, I'm not aware of any particular software that allows you to monitor the audio output of specific applications.

However, if you have an iPhone running iOS 8 and the sound works there, why not just use that to test-run your app? After all, your users are going to be running the app on an actual iPhone, not a simulator. I think the issue you're experiencing must be a bug with the simulator itself, and as long as it has no bearing on the app you eventually distribute to users, I wouldn't waste energy worrying about it.

thanks! i agree. it was more crucial to solve when i wasn't aware you could load in the earlier versions of the iOS emulator because i don't have a non-5s device to test on

im ok with older emu to test older phones, i suppose.

my test bed is oldish 2010 macbook as well so it could be a simple hardware incompatibility unfortunately

thanks!

Glad I could help! Just remember to test your work on your hardware iPhone before distributing it and you should be good to go. :-) Hopefully this bug will be smoothed out as iOS 8 matures.