Sto usando il framework AVAudioPlayer e ho diversi suoni che suonano uno alla volta. Quando un suono è finito, voglio che l'applicazione faccia qualcosa. Ho provato a utilizzare audioPlayerDidFinishPlaying per eseguire l'azione alla fine del primo suono, ma non ho potuto utilizzarlo per il secondo suono perché ho ricevuto un errore di ridefinizione. Posso usare NSTimeInterval per ottenere la durata di un suono?Esegui un'azione quando un suono ha finito di suonare in AVAudioPlayer?
// 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];
}
}
Oh! Sì, intendevo assolutamente dire audioPlayerDidFinishPlaying, non applicationDidFinishLaunching. Scusate. Ho impostato il delegato su se stesso e ha funzionato per il primo suono. Ma il problema non è che audioPlayerDidFinishPlaying non funzioni, il problema è che non so come farlo di nuovo per un secondo suono. Ho due suoni diversi. – Evelyn
A proposito, quando inserisco [player setDelegate: self], ricevo l'avviso: "class 'ViewController' non implementa il protocollo 'AVAudioPlayerDelegate'." È un problema? – Evelyn
È necessario impostare se stessi come delegato di entrambi i suoni. Il suono si identificherà quando ti manda un messaggio delegato, quindi puoi confrontarlo con i suoni che hai a portata di mano. –