博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】iOS:AvPlayer设置播放速度不生效的解决办法
阅读量:6243 次
发布时间:2019-06-22

本文共 3254 字,大约阅读时间需要 10 分钟。

现象:

项目有一个需求是实现视频的慢速播放,使用的是封装的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; } } }

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

from:http://www.jianshu.com/p/555cc5b88f6b

转载地址:http://pmsia.baihongyu.com/

你可能感兴趣的文章
Android Studio 打包生成的 apk 安装包装到手机上闪退
查看>>
Mybatis技术内幕:初始化之加载 mybatis-config
查看>>
mysql与pymysql
查看>>
Fastlane(二):结构
查看>>
vue高阶组件
查看>>
Android消息机制Handler源码分析
查看>>
HashMap JDK1 8源码
查看>>
2018年互联网圈,程序员圈竟然脱单的这么多?
查看>>
数据结构:解读哈夫曼树
查看>>
重新学习web后端开发-003-了解http请求
查看>>
230. Kth Smallest Element in a BST
查看>>
关于Apt注解实践与总结【包含20篇博客】
查看>>
PAT A1004
查看>>
学习webpack4 - 第三方库的使用
查看>>
PAT A1052
查看>>
vue工程全局设置ajax的等待动效
查看>>
Sublime Text3插件安装及问题处理
查看>>
前端如何通过Nginx代理做到跨域访问API接口
查看>>
解析JavaScript"模拟事件"的注意要点
查看>>
HashMap剖析之内部结构
查看>>