簡體   English   中英

使用avplayer時如何提高seek的性能

[英]how to improve the performance of seek when using avplayer

我正在使用AVPlayer創建視頻播放器,但seekToTime方法非常慢。 蘋果應用程序“照片”的搜索性能給我留下了深刻的印象。 有誰知道蘋果是如何做到如此快速的搜索的?

跟線程有關系嗎? 我試圖將seekToTime調用放在調度隊列中,它也無濟於事。

我找到了解決辦法。

如果我使用seekToTime進行清理,它會很慢。 我應該使用的是來自AVPlayerItem稱為stepByCount的方法。

此代碼取自: https : //developer.apple.com/library/archive/qa/qa1820/_index.html

它有點幫助,向前看看起來很順利。 但是向后尋找仍然需要太多時間(這里SeekToTime 僅適用於向前順利工作,向后凍結是解釋原因)。

import AVFoundation
 
class MyClass {
 
    var isSeekInProgress = false
    let player = <#A valid player object #>
    var chaseTime = kCMTimeZero
    // your player.currentItem.status
    var playerCurrentItemStatus:AVPlayerItemStatus = .Unknown
 
    ...
 
    func stopPlayingAndSeekSmoothlyToTime(newChaseTime:CMTime)
    {
        player.pause()
 
        if CMTimeCompare(newChaseTime, chaseTime) != 0
        {
            chaseTime = newChaseTime;
 
            if !isSeekInProgress
            {
                trySeekToChaseTime()
            }
        }
    }
 
    func trySeekToChaseTime()
    {
        if playerCurrentItemStatus == .Unknown
        {
            // wait until item becomes ready (KVO player.currentItem.status)
        }
        else if playerCurrentItemStatus == .ReadyToPlay
        {
            actuallySeekToTime()
        }
    }
 
    func actuallySeekToTime()
    {
        isSeekInProgress = true
        let seekTimeInProgress = chaseTime
        player.seekToTime(seekTimeInProgress, toleranceBefore: kCMTimeZero,
                toleranceAfter: kCMTimeZero, completionHandler:
        { (isFinished:Bool) -> Void in
 
            if CMTimeCompare(seekTimeInProgress, chaseTime) == 0
            {
                isSeekInProgress = false
            }
            else
            {
                trySeekToChaseTime()
            }
        })
    }
 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM