Simple way to play a sound file (.aif)
-
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.
-
Answer:
To play audio on the Mac, you can either use http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html for basic activities, or the more robust http://developer.apple.com/library/mac/#documentation/MusicAudio/Conceptual/CoreAudioOverview/Introduction/Introduction.html framework for more complicated tasks. If you were to use NSSound to achieve this goal, your code would look something like this: NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"replace-with-file-name" ofType:@"aif"]; NSSound *sound = [[NSSound alloc] initWithContentsOfFile:resourcePath byReference:YES]; // Do something with sound, like [sound play] and release. OR NSSound *sound = [NSSound soundNamed:@"replace-with-file-name-without-the-extension"]; // Do something with sound, like [sound play], but don't release (it's autoreleased). If you were to use the second snippet, you would have to ensure that the sound file's extension is aiff and not just aif, since +soundNamed: looks for very specific file extensions; I don't think aif is on that list. Using CoreAudio is more complicated, so if you just want to play it and mess around with very simple settings, just use NSSound
Andy at Stack Overflow Visit the source
Other answers
Basic playback of sound is quite easy. You can use the http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html#//apple_ref/doc/uid/20000393 class to load the file in a few different ways, for example, by name: NSSound * myAwesomeSound = [NSSound soundNamed:@"AwesomeSound"]; This will need to be retained if you want to keep it around. in this case the class will search certain particular directories, including your application bundle, for a sound file with that name. One important note is that the file must have the .aiff (with two f's) extension in order to be found by this method.* You would probably store the sound file in the Resources folder of your project; that's where "media" files are commonly kept. Then you can play it very simply, perhaps using a button press: - (IBAction)playTheSound:(id)sender { NSSound * myAwesomeSound = [NSSound soundNamed:@"AwesomeSound"]; [myAwesomeSound play]; } It's also possible to do some basic transport control: pausing, stopping, checking whether the sound has finished, and so on. Please see the http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Sound/ for details. *It's possible to use other formats, too, of course.
Josh Caswell
Check http://cocoathings.blogspot.com/2013/01/playing-system-sounds.html Here is the list of system sounds.
emkaka
For example, add the AudioToolbox framework, the add the sound file to your resources / project, then add the below to your .h: #import <AudioToolbox/AudioToolbox.h> And then: (after @interface { blah } in your .h) - (IBAction) playSound; Add the following to your .m: (change 'Ping' and 'wav' to the respective file name and type) - (IBAction) playSound { SystemSoundID soundID; NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"Ping" ofType:@"wav"]; AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID); AudioServicesPlaySystemSound(soundID); [soundFile release]; }
buzzkip
You can copy and add your .aif files to your project in Xcode as follows: Command-click on the Classes folder (from within xCode) Select Add > Existing Files Navigate to the directory containing your .aif and select it(them). Click Add. (optional: Check the “Copy items” checkbox & Click Add.) See Apple's examples & great info: http://developer.apple.com/library/ios/#samplecode/SysSound/Introduction/Intro.html & http://developer.apple.com/library/ios/#codinghowtos/AudioAndVideo/_index.html#//apple_ref/doc/uid/TP40007426.
Todd Hopkinson
Related Q & A:
- How to play a sound after a command has finished its execution?Best solution by Ask Ubuntu
- What is the best way to count lines in file?Best solution by Stack Overflow
- Is there any way of getting VHS tapes into a video file on a computer?Best solution by cnet.com
- Is there a way to play the SIMS for free online?Best solution by Yahoo! Answers
- Is there a way to play PC games on a MAC?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.