iOS开发:视频快进、慢进、快退(倍速播放)

1.基本介绍:

/* For releases of OS X prior to 10.9 and releases of iOS prior to 7.0, indicates whether the item can be played at rates greater than 1.0.
Starting with OS X 10.9 and iOS 7.0, all AVPlayerItems with status AVPlayerItemReadyToPlay can be played at rates between 1.0 and 2.0, inclusive, even if canPlayFastForward is NO; for those releases canPlayFastForward indicates whether the item can be played at rates greater than 2.0.
*/
对于10.9之前的OS X版本和7.0之前的iOS版本,指示是否可以大于1.0的速率播放该项目。
从OS X 10.9和iOS 7.0开始,所有状态为AVPlayerItemReadyToPlay的AVPlayerItem都可以以1.0到2.0(含)之间的速率播放,即使canPlayFastForward为NO也不例外; 对于这些发行版,canPlayFastForward指示是否可以大于2.0的速率播放该项目。

2.AVPlayerItem相关属性:

@property (nonatomic, readonly) BOOL canPlayFastForward 
7.0前表示播放速度是否可以在1.0-2.0之间播放。7.0后表示播放速度是否可以大于2.0,即使该属性为NO,播放速度也可以在1.0-2.0之间播放;

@property (nonatomic, readonly) BOOL canPlaySlowForward
是否可以再0.0-1.0之间播放;

@property (nonatomic, readonly) BOOL canPlayReverse
是否可以以-1.0的速度播放;

@property (nonatomic, readonly) BOOL canPlaySlowReverse
是否可以在-1.0 - 0.0之间的速度播放;

@property (nonatomic, readonly) BOOL canPlayFastReverse
是否可以低于-1.0的速度播放;

@property CMTime configuredTimeOffsetFromLive
@property (nonatomic, readonly) CMTime recommendedTimeOffsetFromLive
为配置的实时时间偏移量推荐值。(非高清线路为12 高清线路为18 具体原因还未知。只有M3U8才会有值)

@property (readonly) BOOL canStepForward
是否支持快进,一旦AVPlayerItem的status变为AVPlayerItemStatusReadyToPlay,该值将不会再变;

@property (readonly) BOOL canStepBackward
是否支持快退,一旦AVPlayerItem的status变为AVPlayerItemStatusReadyToPlay,该值将不会再变;
@property (copy) AVAudioTimePitchAlgorithm audioTimePitchAlgorithm 控制倍速播放的质量:
@constant AVAudioTimePitchAlgorithmLowQualityZeroLatency 
可设置速度范围:{0.5, 0.666667, 0.8, 1.0, 1.25, 1.5, 2.0} 低质量,计算成本非常低。 适用于短暂的快进/快退效果,低质量的声音。(这个是默认属性)
@constant AVAudioTimePitchAlgorithmTimeDomain  
可设置速度范围:1/32 to 32.质量适中,计算成本低。 适合声音。
@constant AVAudioTimePitchAlgorithmSpectral  
可设置速度范围:1/32 to 32.最高质量,计算成本最高。 适合音乐。(这个音质最好,快进/慢进的声音正常)
@constant AVAudioTimePitchAlgorithmVarispeed 
可设置速度范围:1/32 to 32.高质量,无音高校正。 音高随速率变化。(用这个,快进会高音、慢进会低音)

3.使用:

快进:AVPlayer .rate > 1
慢放: 0 < AVPlayer .rate < 1
快退: AVPlayer .rate < 0 (m3u8 不能快退)

MP4:如果快进到了缓存不够的地方:KVC监听 AVPlayerItem .isPlaybackBufferEmpty = yes
快退到头会走通知AVPlayerItemDidPlayToEndTimeNotification
m3u8(静态):快进与MP4格式视频一致,可以10倍 20倍播放。
m3u8(动态):快进到了缓存不够的地方,会走通知AVPlayerItemDidPlayToEndTimeNotification。

备注:使用[AVPlayerItem stepByCount:-24];
将AVPlayerItem 向前或向后移动指定的步数,正数前进,负数后退。 每个步数的大小取决于AVPlayerItem启用的AVPlayerItemTracks对象;

4.代码

/**
只能在AVPlayerItem状态为AVPlayerItemStatusReadyToPlay时设置
*/
AVPlayer.rate = 1.00f;//设置正常速率。
AVPlayer.rate = -2.00f;//设置快退速率。
AVPlayer.rate = 32.00f;//设置快进速率。
AVPlayer.rate = 1/32.00f;//设置慢进速率。

注册监听和通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iosMediaPlayerPlayFinish:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.AVPlayerItem];
[self.AVPlayerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
[self.AVPlayerItem addObserver:self forKeyPath:@"playbackBufferEmpty" options:NSKeyValueObservingOptionNew context:nil];
[self.AVPlayerItem addObserver:self forKeyPath:@"playbackLikelyToKeepUp" options:NSKeyValueObservingOptionNew context:nil];
/**
 *  kVC监听播放状态
 *
 *  @param keyPath
 *  @param object
 *  @param change
 *  @param context
 */
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([self.AVPlayerItem isEqual:object] && [@"status" isEqualToString:keyPath]){
        if (self.AVPlayerItem.status == AVPlayerItemStatusReadyToPlay) {
            //在这里设置速率才有效 或者播放出来之后再设置
        }
    }
    else if([self.AVPlayerItem isEqual:object] && [@"playbackBufferEmpty" isEqualToString:keyPath]){
        if ([self.AVPlayerItem isPlaybackBufferEmpty]) {
            NSLog(@"监听当缓存不够,视频加载不出来的情况");
        }
    }
    else if ([self.AVPlayerItem isEqual:object] && [@"playbackLikelyToKeepUp" isEqualToString:keyPath]){
        if ([self.AVPlayerItem isPlaybackLikelyToKeepUp]) {
            NSLog(@"监听缓存足够播放的状态");
        }
    }
}

你可能感兴趣的:(iOS开发:视频快进、慢进、快退(倍速播放))