I am trying to segue
to the next view at the end of a short music clip. There aren't any errors but when I run it, the NSLog
message never appears and the segue never happens. Why doesn't the delegate
know when the audio ends?
This is the .h file:
@interface RecordingViewController : UIViewController
<AVAudioPlayerDelegate> {
AVAudioPlayer *player;
NSArray *recordingsArray;
int randomizedArtist;
}
Here's some code from the viewDidLoad
:
// set self as AVAudioPlayerDelegate
[player setDelegate:self];
// prep Audio Player
@try {
NSError *myError;
path = [[NSBundle mainBundle] pathForResource:recordingsArray[randomizedArtist] ofType:@"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&myError];
if (!player) {
NSLog(@"viewDidLoad:error %@", [myError localizedDescription]);
NSLog(@"viewDidLoad:failure reason %@", [myError localizedFailureReason]);
NSLog(@"viewDidLoad:recovery option %@", [myError localizedRecoveryOptions]);
NSLog(@"viewDidLoad:recovery suggestions %@", [myError localizedRecoverySuggestion]);
}
}
@catch (NSException *exception){
NSLog(@"Exception %@ occurred", exception.name);
NSLog(@"Exception reason %@", exception.reason);
NSLog(@"Exception userInfo %@", exception.userInfo);
// make pop up alert
}
@finally{
NSLog(@"Finally on reading URL file called");
}
[player prepareToPlay];
Here is where I call the player to start playing:
- (IBAction)playButton:(id)sender {
[player play];
}
And here is where I call the segue:
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(@"Player did finish playing");
[self performSegueWithIdentifier:@"quizVCSegue" sender:self];
}
Check this code if its work
@interface ViewController()..,AVAudioPlayerDelegate
Allocate the AVPlayer
NSError *error;
_audioPlayerRecord = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
_audioPlayerRecord.delegate = self;
[_audioPlayerRecord prepareToPlay];
}
and
#import <AVFoundation/AVFoundation.h>