Jerky playback from avplayer on Applying Rate greater than 2x

后端 未结 4 655
难免孤独
难免孤独 2021-01-07 00:15

I want to tweak Avplayer rate , I able to do with help of

[_avplayer play];
[_avplayer setRate:1.5];

Also disabled audio tracks , it is ru

4条回答
  •  逝去的感伤
    2021-01-07 00:48

    As @Rhythmic suggested these ways can be implemented but these all are kind of hassle . I googled it more and found a way and it is working very fine no jerk or choppy .

    Just do not set rate , set Rate like this . First create instance of AVPlayer , AVPlayerItem and AVAsset .

      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];
            self.avRefPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    
            self.avRefPlayerLayer = [AVPlayerLayer layer];
    
            [self.avRefPlayerLayer setPlayer:self.avRefPlayer];
            [self.avRefPlayerLayer setFrame:_refrencedView.bounds];
            [self.avRefPlayerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
    

    this code can even support more than 2x or 4x speed easily .

提交回复
热议问题