Pregunta

He estado buscando por un tiempo cómo jugar sonido en el iPhone, y yo creo que es algo a lo largo de las líneas de:

[[NSSound soundNamed:@"cat.mp3"] play];

Pero el NSSound está en la AppKit ... alguna sugerencia? Sé que hay una respuesta muy simple a este, pero hoy mis búsquedas no prestan ningún resultado ...

¿Fue útil?

Solución

la nueva clase AVFoundation es la forma más fácil de reproducir sonido en el iPhone, aunque es para el firmware 2.2 solamente:

AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];  
[audioPlayer play]; 

sólo tiene esta importación en la clase que lo utiliza:

#import <AVFoundation/AVAudioPlayer.h>

Otros consejos

sonidos del sistema Audio Toolbox son grandes para la reproducción asíncrona corto.

// Initialize
CFBundleRef mainBundle;
mainBundle = CFBundleGetMainBundle ();

// Init each sound
CFURLRef      tapURL;
SystemSoundID tapSound;
tapURL = CFBundleCopyResourceURL(mainBundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(tapURL, &tapSound);

// Play the sound async
AudioServicesPlaySystemSound(tapSound);

No he llegado MP3 para trabajar con AudioServicesPlaySystemSound. Pero juega archivos AIF perfectamente.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top