volumen solo silencio reparar pone funciona descompuso como botones boton bloqueo arreglar iphone ios4 memory audio

solo - reparar boton silencio iphone 6



Superposición de sonido con múltiples pulsaciones de botones (6)

Agregue un cheque para ver si el reproductor ya está jugando al comienzo de cada método:

if (theAudio.playing == YES) { [theAudio stop]; }

AVAudioPlayer Class Reference

Cuando presiono un botón, luego presiono otro, los sonidos se superponen. ¿Cómo puedo arreglar eso para que el primer sonido se detenga cuando se presiona otro?

- (void)playOnce:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio setDelegate: self]; [theAudio setNumberOfLoops:0]; [theAudio setVolume:1.0]; [theAudio play]; } - (void)playLooped:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [theAudio setDelegate: self]; // loop indefinitely [theAudio setNumberOfLoops:-1]; [theAudio setVolume:1.0]; [theAudio play]; [theAudio release]; }


Declare su AVAudioPlayer en el encabezado viewController (no coloque uno nuevo cada vez que reproduzca un sonido). De esa forma tendrá un puntero que puede usar en un método StopAudio.

@interface myViewController : UIViewController <AVAudioPlayerDelegate> { AVAudioPlayer *theAudio; } @property (nonatomic, retain) AVAudioPlayer *theAudio; @end @implementation myViewController @synthesize theAudio; - (void)dealloc { [theAudio release]; } @end - (void)playOnce:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; if(!theAudio){ theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL]; } [theAudio setDelegate: self]; [theAudio setNumberOfLoops:0]; [theAudio setVolume:1.0]; [theAudio play]; } - (void)playLooped:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; if(!theAudio){ theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath:path] error:NULL]; } [theAudio setDelegate: self]; // loop indefinitely [theAudio setNumberOfLoops:-1]; [theAudio setVolume:1.0]; [theAudio play]; } - (void)stopAudio { [theAudio stop]; [theAudio setCurrentTime:0]; }

también asegúrese de leer los documentos de Apple


El código que ha publicado solo reproducirá un sonido, el primer sonido enviado al método.

Si solo desea reproducir un sonido variable, después de detener el reproductor, suelte el audio y luego configure el nuevo sonido.


En su método playOnce, la variable ''ruta'' no se utiliza: elimínela para eliminar su mensaje de advertencia. Your playOnce no configura nada para jugar, así que no estoy seguro de cómo se supone que funciona, a menos que llame a playLooped primero. También debe llamar a prepareToPlay después del initWithContentsOfUrl.

- (void)playOnce:(NSString *)aSound { NSString *path = [[NSBundle mainBundle] pathForResource:aSound ofType:@"caf"]; if (theAudio && [theAudio isPlaying]) { [theAudio stop] } else { theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL]; [theAudio prepareToPlay]; [theAudio setDelegate: self]; [theAudio setNumberOfLoops: 1]; [theAudio setVolume: 1.0]; [theAudio play]; } }


Necesitará utilizar un valor BOOL para que esto funcione correctamente.

en tu archivo .m ANTES de @implementation pon esto:

static BOOL soundIsPlaying = NO;

Entonces su IBAction necesita verse así:

- (IBAction)play { if (soundIsPlaying == YES) { [theAudio release]; soundIsPlaying = NO; } else if (soundIsPlaying == NO) { NSString *path = [[NSBundle mainBundle] pathForResource:@"SOUNDFILENAME" ofType:@"wav"]; theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; theAudio.delegate = self; theAudio.volume = 1.0; theAudio.numberOfLoops = 0; [theAudio play]; soundIsPlaying = YES; } }

eso es honesto al respecto. Parará los sonidos cuando se presione otro botón.


Probablemente puedas hacer algo como lo siguiente:

(void) playLooped: (NSString * ) aSound { NSString * path = [[NSBundle mainBundle] pathForResource: aSound ofType: @"caf"]; //stop audio player from playing so the sound don''t overlap if([theAudio isPlaying]) { [theAudio stop]; //try this instead of stopAudio } if (!theAudio) { theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL]; } [theAudio setDelegate: self]; // loop indefinitely [theAudio setNumberOfLoops: -1]; [theAudio setVolume: 1.0]; [theAudio play]; }