Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
Andy Avatar asked May 16 '11 20:05

Andy


People also ask

How do I play .AIF files?

You can play AIFF and AIF files with AIFF Windows Media Player, Apple iTunes, Apple QuickTime, VLC, Media Player Classic, and presumably most other multi-format media players. Macintosh PCs can open AIFF and AIF files with those Apple programs, as well, just as with Toast.

How do you play sound files on MSP Max?

Play audio files: sfplay~ The audio output of sfplay~ can be sent directly to dac~ or ezdac~, and/or anywhere else in MSP. Click on the openmessage box marked 'Set the current file', and open the audio file you have just recorded. Then (with audio on) click on the toggle marked 'Play/Stop' to hear your file.

How do I play AIFF files on iPhone?

Question: Q: Play AIFF Files on iPhone Simply save the file to a Note, and it'll play.


2 Answers

To play audio on the Mac, you can either use NSSound for basic activities, or the more robust CoreAudio 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

like image 123
Itai Ferber Avatar answered Sep 19 '22 16:09

Itai Ferber


Basic playback of sound is quite easy.

You can use the NSSound 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 Sound Programming Topics Guide for details.


*It's possible to use other formats, too, of course.

like image 45
jscs Avatar answered Sep 20 '22 16:09

jscs