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

現(xiàn)象:

項(xiàng)目有一個(gè)需求是實(shí)現(xiàn)視頻的慢速播放,使用的是封裝的AvPlayer,但是設(shè)置時(shí)發(fā)現(xiàn)比如設(shè)置rate為0.5,0.1,0.01都是一樣的速度,非常疑惑.后來經(jīng)過查找資料,發(fā)現(xiàn)iOS
10對這個(gè)API進(jìn)行了更新

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)這個(gè)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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容