iOS:AvPlayer设置播放速度不生效的解决办法

现象:

项目有一个需求是实现视频的慢速播放,使用的是封装的AvPlayer,但是设置时发现比如设置rate为0.5,0.1,0.01都是一样的速度,非常疑惑.后来经过查找资料,发现iOS
10对这个API进行了更新

iOS10之前官方API对AvPlayer rate属性的解释是
/*! 
@property  rate
@abstract  Changes the playback rate of the input signal
@discussion   A value of 2.0 results in the output audio playing one octave higher.A value of 0.5, results in the output audio playing one octave lower.
Range: 0.5 -> 2.0
Default: 1.0
Mixer: AVAudioEnvironmentNode
*/

由上,我们可以发现这个rate只是支持0.5-2倍的

iOS10更新后对AvPlayer rate属性的解释是
/*!
 @property  rate
 @abstract  Indicates the desired rate of playback; 0.0 means "paused", 1.0 indicates a desire to play at the natural rate of the current item.
 @discussion
 Setting the value of rate to 0.0 pauses playback, causing the value of timeControlStatus to change to AVPlayerTimeControlStatusPaused.
 Setting the rate to a non-zero value causes the value of timeControlStatus to become either AVPlayerTimeControlStatusWaitingToPlayAtSpecifiedRate or AVPlayerTimeControlStatusPlaying, depending on whether sufficient media data has been buffered for playback to occur and whether the player's default behavior of waiting in order to minimize stalling is permitted. See discussion of AVPlayerTimeControlStatus for more details.
 
 AVPlayer can reset the desired rate to 0.0 when a change in overall state requires playback to be halted, such as when an interruption occurs on iOS, as announced by AVAudioSession, or when the playback buffer becomes empty and playback stalls while automaticallyWaitsToMinimizeStalling is NO.

 The effective rate of playback may differ from the desired rate even while timeControlStatus is AVPlayerTimeControlStatusPlaying, if the processing algorithm in use for managing audio pitch requires quantization of playback rate. For information about quantization of rates for audio processing, see AVAudioProcessingSettings.h. You can always obtain the effective rate of playback from the currentItem's timebase; see the timebase property of AVPlayerItem.
 */

在代码中设置小于0.5的值一直不生效,查找资料,好像只是支持

I actually had a ticket with Apple DTS open for this issue and a bug filed. The only supported values are 0.50, 0.67, 0.80, 1.0, 1.25, 1.50, and 2.0. All other settings are rounded to nearest value.

详见 解决办法

这里面有人提供了这样一种办法:

I found that smaller values are indeed supported, but all tracks in the AVPlayerItem have to support the speed. However, Apple does not provide a property on individual tracks that would indicate what rates are supported, there is only the property canPlaySlowForward on AVPlayerItem.

What i found is, that AVPlayerItems with an audio track cannot play at rates slower than 0.5. However, if there is only a video track, the rate can have an arbitrary small value like 0.01. I will try to write a category that checks on-the-fly which values are supported and disable unsupported tracks if needed.


//I wrote a function which you can call whenever you want to set the rate for video below 0.5. It enables/disables all audio tracks.
- (void)enableAudioTracks:(BOOL)enable inPlayerItem:(AVPlayerItem*)playerItem
{
    for (AVPlayerItemTrack *track in playerItem.tracks)
    {
        if ([track.assetTrack.mediaType isEqual:AVMediaTypeAudio])
        {
            track.enabled = enable;
        }
    }
}

我使用此方法解决了我的问题,非常感谢!!!!!特此记录,备忘!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,457评论 0 23
  • 我在南方,心在北 你城市下的雨 是我心在思念你而落的泪 你的笑容很美丽 像微风拂过 沁人心脾 遇见你的那一刻 我便...
    小谷同学_阅读 4,538评论 6 5
  • 最新预告 1 白嘉程的母亲赵德芳自从女儿一家出事后,就一直住在这家精神疗养院里。 当年,肖寒一家刚到泰国后,就住在...
    xiaohao0725阅读 3,916评论 0 0
  • 最近,看小小鱼背诗,蛮有意思,二三天会背一首,不过经常会串,目前仅仅只是要求她会背而已,也没有按照儿童书籍的诗词...
    冒泡的小鱼阅读 2,548评论 0 0
  • 经历了几年双十一的剁手经历,去年双十一就给马爸爸贡献了8万左右! 不要问我是怎么花的,我也不知道。 请点击此处输入...
    育儿手账阅读 3,328评论 0 2