iOS:AvPlayer設(shè)置播放速度不生效的解決辦法

現(xiàn)象:

項目有一個需求是實現(xiàn)視頻的慢速播放,使用的是封裝的AvPlayer,但是設(shè)置時發(fā)現(xiàn)比如設(shè)置rate為0.5,0.1,0.01都是一樣的速度,非常疑惑.后來經(jīng)過查找資料,發(fā)現(xiàn)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
*/

由上,我們可以發(fā)現(xiàn)這個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.
 */

在代碼中設(shè)置小于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;
        }
    }
}

我使用此方法解決了我的問題,非常感謝!!!!!特此記錄,備忘!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末职辨,一起剝皮案震驚了整個濱河市戈二,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌觉吭,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件伴鳖,死亡現(xiàn)場離奇詭異徙硅,居然都是意外死亡,警方通過查閱死者的電腦和手機嗓蘑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來休吠,“玉大人,你說我怎么就攤上這事瘤礁。” “怎么了柜思?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長号枕。 經(jīng)常有香客問我陨享,道長,這世上最難降的妖魔是什么抛姑? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮皿桑,結(jié)果婚禮上蔬啡,老公的妹妹穿的比我還像新娘。我一直安慰自己箱蟆,他們只是感情好,可當我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布近零。 她就那樣靜靜地躺著抄肖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪漓摩。 梳的紋絲不亂的頭發(fā)上入客,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天腿椎,我揣著相機與錄音夭咬,去河邊找鬼。 笑死卓舵,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的裹虫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼筑公,長吁一口氣:“原來是場噩夢啊……” “哼尊浪!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起拇涤,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤工育,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后如绸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡搪泳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年扼脐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片瓦侮。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖方妖,靈堂內(nèi)的尸體忽然破棺而出罚攀,到底是詐尸還是另有隱情雌澄,我是刑警寧澤,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布镐牺,位于F島的核電站魁莉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏沛厨。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一宅粥、第九天 我趴在偏房一處隱蔽的房頂上張望电谣。 院中可真熱鬧,春花似錦剿牺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽湃崩。三九已至,卻和暖如春攒读,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背薄扁。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工邓梅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人震放。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓驼修,卻偏偏與公主長得像诈铛,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子幢竹,可洞房花燭夜當晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,569評論 0 23
  • 我在南方焕毫,心在北 你城市下的雨 是我心在思念你而落的淚 你的笑容很美麗 像微風拂過 沁人心脾 遇見你的那一刻 我便...
    小谷同學(xué)_閱讀 982評論 6 5
  • 最新預(yù)告 1 白嘉程的母親趙德芳自從女兒一家出事后驶乾,就一直住在這家精神療養(yǎng)院里。 當年级乐,肖寒一家剛到泰國后,就住在...
    xiaohao0725閱讀 637評論 0 0
  • 最近撒轮,看小小魚背詩,蠻有意思题山,二三天會背一首,不過經(jīng)常會串故痊,目前僅僅只是要求她會背而已,也沒有按照兒童書籍的詩詞...
    冒泡的小魚閱讀 319評論 0 0
  • 經(jīng)歷了幾年雙十一的剁手經(jīng)歷抡柿,去年雙十一就給馬爸爸貢獻了8萬左右! 不要問我是怎么花的洲劣,我也不知道。 請點擊此處輸入...
    育兒手賬閱讀 482評論 0 2