When using AVAudioSequencer with AVAudioEngine, how do I know when the sequencer has finished playing? I don't see anything that gives me a completion handler.
AVAudioSequencer - when has play finished?
Maybe a little late but here is a workaround
Once the data (or URL ) is loaded I determine the seconds in the tracks; I then issue a Timer at the computed time which call a delegate method. The delegate triggers a method to deactivate the timer
-(NSTimeInterval)totalLengthInSeconds {
NSTimeInterval result = 0.5;
for(AVMusicTrack *track in _audioSequencer.tracks) {
result += track.lengthInSeconds;
}
return result;
}
-(void) example {
[_audioSequencer prepareToPlay];
NSTimeInterval totalLengthInSeconds = [self totalLengthInSeconds];
[_audioSequencer startAndReturnError:&error];
_audioSequencer.currentPositionInSeconds = 0;
_sequencerTimer = [NSTimer scheduledTimerWithTimeInterval:totalLengthInSeconds repeats:NO block:^(NSTimer * _Nonnull timer) {
[self.delegate midiPlayerDidEnd];
}];
}
BlockQuote
Replaces the previous reply:
Once the data (or URL ) is loaded I determine the seconds in the tracks; I then issue a Timer at the computed time which calls a delegate method. The delegate triggers a method to deactivate the timer
-(NSTimeInterval)totalLengthInSeconds {
NSTimeInterval result = 0.5;
for(AVMusicTrack *track in _audioSequencer.tracks) {
result = MAX( result, track.lengthInSeconds);
}
return result;
}
-(void) example {
[_audioSequencer prepareToPlay];
NSTimeInterval totalLengthInSeconds = [self totalLengthInSeconds];
[_audioSequencer startAndReturnError:&error];
_audioSequencer.currentPositionInSeconds = 0;
_sequencerTimer = [NSTimer scheduledTimerWithTimeInterval:totalLengthInSeconds repeats:NO block:^(NSTimer * _Nonnull timer) {
[self.delegate midiPlayerDidEnd];
}];
}