umair.saeed
umair.saeed

Reputation: 131

AVAudioPlayer currentTime lag issue

When I change the currentTime of AVAudioPlayer after pausing the player, it gives a lag (some time in positive and some time in negative).

[self.bookAudioPlayer pause];
[self.bookAudioPlayer setCurrentTime:[currentPage.audioStartTime doubleValue]];
[self.bookAudioPlayer prepareToPlay];

When I print the currentTime and audioStartTime it prints values with slight difference. For example,

audioStartTime : 2.203665, currentTime : 2.194286

audioStartTime : 137.521347, currentTime : 137.508571

I have tried to fix it using the following code but results stay the same.

- (void)fixSetCurentTime:(NSTimeInterval)newTime {
    self.currentTime = newTime;
    
    if (self.currentTime != newTime) {
        [self prepareToPlay];
        self.currentTime = newTime;
    }
}

Has anyone experienced this issue? Any pointers for a possible fix?

Upvotes: 4

Views: 925

Answers (1)

Mustafa
Mustafa

Reputation: 20564

Note that the MP3 audio frames are about 26 milliseconds long. So, block-based audio compression formats are only (re)startable on time-quantized block boundaries... And the nearest block start might be earlier (or later) in time.

Credit: Apple Developer Forum User (Reference)

Upvotes: 4

Related Questions