2012-05-24 20 views

Respuesta

11

Si desea reproducir un sonido corto (menos de 30 segundos), puede hacerlo fácilmente así:

Nota: Vas a tener añadir marco AudioToolbox y la importación (#import <AudioToolbox/AudioToolbox.h>)

SystemSoundID mySSID; 

NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"]; 
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID); 

AudioServicesPlaySystemSound(mySSID); 

también tenga en cuenta que el archivo puede ser:

  • no más de 30 segundos de duración
  • En PCM lineal o ima4 formato (IMA/ADPCM)
  • Empaquetado en una .caf, .aif o archivo .wav
+0

¡¡Genial !! Pero, ¿es posible reproducir algún sonido del sistema? No quiero cargar anythink nueva ... somethink como standart botón iphone sonido de clic – Radislav

+0

No estoy seguro de eso ... Aunque sé con seguridad que se puede utilizar la 'vibra' _system Sound_. Si encuentro algo, te lo haré saber. – Alladinian

+0

1 para una mejor solución para el sistema de corta suena –

3

Debe utilizar AVAudioPlayer.

Hay un gran tutorial sobre el uso de here AVAudioPlayer para reproducir sonidos. Un ejemplo muy simple de su uso:

NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath]; 

NSError *error; 
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; 

if (audioPlayer == nil) 
    NSLog([error description]); 
else 
    [audioPlayer play]; 
+0

Gran !! Pero, ¿es posible reproducir algún sonido del sistema? No quiero cargar nada nuevo ... Somethink como el botón estándar del iphone haga clic en sonido – Radislav

0
NSString *path = [[NSBundle mainBundle] pathForResource:@"gorilla 2" ofType:@"mp3"]; 
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; 

theAudio.delegate=self; 
[theAudio play]; 
Cuestiones relacionadas