When I take the duration of an audio file before playing it with:
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
...
[audioPlayer prepareToPlay];
duration = audioPlayer.duration;
I get a duration of for example 16.71s. Then when taking samples every 0.2s while playing, the duration changes. It changes every 1.2 to 1.6 seconds to: 16.71s, 17.02s, 17.23s, 17.33s, 17.38s, 17.43s, 17.38s, 17.29s, 17.31s, then stays at 17.51s for the last 2.5 seconds. So it goes up and down.
I sample in a method that updates a slider position, and also shows the elapsed time and total duration. You can imagine it looks really weird to see the duration (which is int-truncated) go from 16 to 17 while playing. Additionally, the slider position will be off with all this drifting.
Does anyone have an idea why this is?
Now that we're talking about duration: Does anyone know why audio player.duration
can return values that are about twice the actual duration when [audioPlayer prepareToPlay]
is omitted?
AVAudioPlayer
. The final value is also always the same: 17.51s. But when I replay with the same audio player all values are 17.51s. – Raffo@{ AVEncoderAudioQualityKey : @(AVAudioQualityMedium), AVNumberOfChannelsKey : @(1), AVFormatIDKey : @(kAudioFormatMPEG4AAC) }
. Would be surprised if the true/correct duration of AAC is not in the format. – Raffo