Hacer una acción cuando el sonido ha terminado de jugar en AVAudioPlayer?
-
12-09-2019 - |
Pregunta
Estoy utilizando el marco AVAudioPlayer, y tengo varios sonidos que se reproducen uno a la vez. Cuando se termina una reproducción de sonido, quiero que la aplicación pueda hacer algo. He intentado utilizar audioPlayerDidFinishPlaying para realizar la acción al final del primer sonido, pero que no podía usar que por segundo sonido porque me ha dado un error redefinición. ¿Puedo utilizar NSTimeInterval para obtener la duración de un sonido?
// ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController : UIViewController {
UIButton *begin;
UIWindow *firstTestWindow;
UIWindow *secondTestWindow;
AVAudioPlayer *player;
NSTimeInterval duration;
}
@property (nonatomic, retain) IBOutlet UIButton *begin;
@property (nonatomic, retain) IBOutlet UIWindow *firstTestWindow;
@property (nonatomic, retain) IBOutlet UIWindow *secondTestWindow;
@property (nonatomic, retain) AVAudioPlayer *player;
@property (readonly) NSTimeInterval duration;
- (IBAction)begin:(id)sender;
- (IBAction)doTapScreen:(id)sender;
@end
//ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize begin, player, firstTestWindow, secondTestWindow;
//first sound
- (IBAction)begin:(id)sender; {
[firstTestWindow makeKeyAndVisible];
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Tone1"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
[self.player play];
}
//If user does not do anything by the end of the sound go to secondWindow
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
if (flag==YES) {
[secondWindow makeKeyAndVisible];
}
}
//second sound
- (IBAction)doTapScreen:(id)sender {
//When user touches the screen, either play the sound or go to the next window
if (self.player.playing) {
[self.player stop];
[thirdWindow makeKeyAndVisible];
}
else {
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Tone2"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
[self.player play];
}
}
Solución
Cuando un sonido se terminó de juego, quiero que la aplicación pueda hacer algo.
A continuación, establezca a sí mismo como el delegado y responder a audioPlayerDidFinishPlaying:successfully:
.
En el fragmento de código que mostró, que ha hecho el paso 2, pero no. Paso 1: Usted no se está instalando como delegado
He intentado utilizar
applicationDidFinishLaunching
para realizar la acción al final del primer sonido ...
pedo cerebro? Ese método no tiene nada que puedo ver que ver con los sonidos de juego.
..., pero no pude usar que por segundo sonido porque me ha dado un error redefinición.
¿Eh? ¿Se implementa el método de dos veces o algo en lugar de simplemente comparando el objeto jugador en el cuerpo del método?
Sería de gran ayuda si usted mostró el mensaje de error exacto que tienes.
Otros consejos
Swift 3:
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("sound file finished")
}