Friday, October 21, 2011

Back to Back Gapless Audio Playback with AVAudioPlayer on the iPhone

There are two ways that I know of to get two audio files from AVAudioPlayer to play one after the other. The first, and the one I have seen recommended elsewhere, is to use audioPlayerDidFinishPlaying as follows:

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
    if ([player isEqual:self.firstAudioFile]) {
        [self.secondAudioFile play];

However this can cause a gap in playback between the two audio files. One way to avoid this and avoid use of this method completely is to schedule the second one to play when the first is done, like this:

[self.firstAudioFile play]
NSTimeInterval now = self.firstAudioFile.deviceCurrentTime;
[self.secondAudioFile playAtTime: now + self.firstAudioFile.duration];

Hope this helps people searching around for a solution to this.

The Apple documentation for AVAudioPlayer:


  1. Can you please post a complete tutorial or create an attach an Xcode project? It would be useful for me.

    Thank you

  2. This still leaves a noticeable brake between songs.