I’m making an app for Mac OS X using Xcode, and I want it to play a alert sound when something happens.
What is the simplest code for playing a sound in Objective-C/Cocoa? Note that my sound file is an .aif.
Also, where do I put the sound file inside my project?
Thank you.
To play audio on the Mac, you can either use
NSSoundfor basic activities, or the more robustCoreAudioframework for more complicated tasks.If you were to use
NSSoundto achieve this goal, your code would look something like this:OR
If you were to use the second snippet, you would have to ensure that the sound file’s extension is
aiffand not justaif, since+soundNamed:looks for very specific file extensions; I don’t thinkaifis on that list.Using
CoreAudiois more complicated, so if you just want to play it and mess around with very simple settings, just useNSSound