iosobjective-cavplayer

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


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?


Solution

  • 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];