How to achieve a playback rate of 4.0 with AVPlayer in iOS?

3.4k views Asked by At

player.rate = 4.0 doesn't work for me.

Is it a good way to achieve rate of 4.0 with AVPlayer, or there is a better approach?

1

There are 1 answers

1
user3480295 On BEST ANSWER

Use [AVComposition scaleTimeRange:toDuration:] to produce a fast-motion AVAsset.

float rate = 4.0;
AVAsset *asset = [AVAsset assetWithURL:someURL];
AVMutableComposition *composition = [AVMutableComposition composition];
NSError *error = nil;
[composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                     ofAsset:asset
                      atTime:kCMTimeZero error:&error];
[composition scaleTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                 toDuration:CMTimeMultiplyByFloat64(asset.duration, 1 / rate)];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:composition];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];