-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[self NextHeading]; // this plays an mp3 file
[self NextHeadingMeaning]; // this plays an Mp3 file
}
Only [self NextHeadingMeaning] method is called and NextHeading method is missed each time
-(IBAction) NextHeading{
;
NSString *Filename = [[NSString alloc]initWithFormat:@"CH%@S%@",Heading,Meaning];
Filepath = [[NSBundle mainBundle]pathForResource:Filename ofType:@"mp3"];
audio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:Filepath] error:NULL];
audio.delegate = self;
;
[Filename autorelease];
}
-(IBAction) NextHeadingMeaning {
;
NSString *Filename = [[NSString alloc] initWithFormat:@"CH%@S%@",bold**Chapter**bold, Meaning];
Filepath = [[NSBundle mainBundle]pathForResource:Filename ofType:@"mp3"];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:Filepath] error:NULL];
audio.delegate = self;
;
[Filename autorelease];
}
Why is this happening and how can I resolve it ?
Please advice, thanks in advance.
You just used a single iVar (
audio) as an player, and when you sendNextHeading&NextHeadingMeaningmessage, theaudioinit with yoursound_1.mp3file firstly (it’ll take some seconds if the mp3 file is big), then at the next moment (your first mp3 file might not inited, or has inited, but stopped followed by next message), you redo the init action with another mp3 file (sound_2.mp3), and finally, when the second mp3 file init done,audioplayssound_2.mp3. That’s why you think theNextHeadingis skipped.So, to solve this problem, you can use a
NSMutableArrayiVar (e.g.audioPlayers), and create a localaudiofor bothNextHeading&NextHeadingMeaning, and push it toaudioPlayers.And I think it is better to preload sound files if you can. 🙂
EDIT:
There’s a
playAtTime:method instead ofplay, you can delay the second audio player’s playing time by this method, just like this:delayis in seconds (NSTimeInterval).