Manera sencilla de reproducir un archivo de sonido (.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?

Gracias por su atención.

preguntado el 16 de mayo de 11 a las 20:05

4 Respuestas

To play audio on the Mac, you can either use NSSound for basic activities, or the more robust CoreAudio framework for more complicated tasks.

Si tuvieras que usar 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.


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 y no solo aif, Desde +soundNamed: looks for very specific file extensions; I don't think aif está en esa lista.

Usar CoreAudio is more complicated, so if you just want to play it and mess around with very simple settings, just use NSSound

contestado el 17 de mayo de 11 a las 00:05

Thanks, can you give me an example of how to use NSSound in the code ? - Andy

Thanks it worked, just one more question: I sound was for example 1min and I release it, does it keep playing ? - Andy

Andy, the NSSound Class Reference states that a sound will stop playing once it has been deallocated: "Upon deallocation, a sound object stops playback of the sound (as needed) so that it can free up the corresponding audio resources." So if you create a sound, start playing it, then release it before it has finished playing, the sound will stop. - Itai Ferber

Basic playback of sound is quite easy.

Puede utilizar el 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 (con Digital XNUMXk 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 para más detalles.

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

contestado el 17 de mayo de 11 a las 01:05

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);
    [soundFile release];

contestado el 17 de mayo de 11 a las 00:05

You can copy and add your .aif files to your project in Xcode as follows:

  1. Command-click on the Classes folder (from within xCode)
  2. Select Add > Existing Files
  3. 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: aquí & aquí.

contestado el 17 de mayo de 11 a las 00:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.