I use AVPlayer's -(id)addPeriodicTimeObserverForInterval: queue: usingBlock:
method to update UI up to playback progress. However, my progress bar never reaches end.
CMTime duration = self.player.currentItem.asset.duration;
float totalSeconds = (Float64)(duration.value * 1000) / (Float64)(duration.timescale);
NSLog(@"duration: %.2f", totalSeconds);
__weak __typeof(self) welf = self;
_mTimeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(10, 1000)
queue:NULL // main queue
usingBlock:^(CMTime time) {
float totalSeconds = (Float64)(time.value * 1000) / (Float64)(time.timescale);
NSLog(@"progress %f", totalSeconds);
}];
logs:
App[2373:792179] duration: 3968.00
hit play button
App[2373:792179] progress 0011.176
App[2373:792179] progress 0021.175
...
App[2373:792179] progress 3701.319
App[2373:792179] progress 3704.000
Should not I expect last number to be 3968.0
?
Audio is streamed from server.
EDIT
Last progress number is ALWAYS duration - 0.264 sec
whatever actual duration length is.
This is so strange, I wish we could use emoticons on SO.
Good question. Try using CMTimeGetSeconds(time)
instead of calculating the total seconds yourself.
Additionally, try to use CMTimeMakeWithSeconds(10.0, NSEC_PER_SEC)
to create the time for the periodic time observer.
This post helped me a lot to wrap my head around the enigma that is CMTime: https://warrenmoore.net/understanding-cmtime
The docs clearly state:
You must retain the returned value as long as you want the time observer to be invoked by the player.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.