Reputation: 103
I'm simply trying to do something when an audio file has finished playing, but it doesn't seem to work.
In my .h file I've got this:
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreAudio/CoreAudioTypes.h>
@interface soundViewController : UIViewController
<AVAudioPlayerDelegate>
{
//various outlets
}
@property (strong, nonatomic) AVAudioPlayer *audioPlayer;
@end
And in the .m file I've got:
#import "soundViewController.h"
@interface soundViewController ()
@end
@implementation soundViewController
@synthesize audioPlayer;
And the method's:
- (void) playWord{
Word *currentWord = [[Word alloc]init];
currentWord = [testWords objectAtIndex:wordNum-1];
NSString *item = [currentWord word];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@.mp3", [[NSBundle mainBundle] resourcePath], item]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil){
NSLog(@"error in audioPlayer");
}
else{
[audioPlayer play];
NSLog(@"Playing word");
}
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
if (flag==YES){
NSLog(@"Finished!");
}
}
So... it all works swimmingly (apart from the fact that the sound file won't work if it starts with a capital letter), but why am I not seeing the log message "Finished" when the sound finishes?
Thanks for your help, Morten
Upvotes: 1
Views: 1537
Reputation: 18363
You actually need to set the delegate of the player after you instantiate it, like:
audioPlayer.delegate = self;
Hope this helps!
Upvotes: 2