Why does the AVPlayerItem canPlayFastForward method return False?

I really want to implement fast forward and rewind using AVFoundation.

As far as I know, I can only play 0.0 ~ 2.0 with AVPlayer if AVPlayerItem canPlayReverse and canPlayFastForward returns False.

But I need -1.0, and also a speed above 2.0.

My problem is that I just cannot find when and why the results are false.

There is no mention of when canPlayFastForward returns false in an Apple document.

Can someone explain when and why the results of canPlayFastForward and canPlayReverse are false , and how can I change this to true ?

+4
source share
1 answer

Perhaps you are checking AVPlayerItem canPlayReverseor canPlayFastForwardbefore the property AVPlayerItem statuschanges to .readToPlay. If you do, you will always receive false.

Do not do this:

import AVFoundation

let anAsset = AVAsset(URL: <#A URL#>)
let playerItem = AVPlayerItem(asset: anAsset)
let canPlayFastForward = playerItem.canPlayFastForward
if (canPlayFastForward){
   print("This line won't execute")
}

Observe the property instead AVPlayerItem status. The following is Apple's documentation :

AVPlayerItem . AVPlayerItem.canPlayFastForward YES ( , ) . , , AVPlayerItem.status ().

import AVFoundation
dynamic var songItem:AVPlayerItem! //Make it instance variable

let anAsset = AVAsset(URL: <#A URL#>)
let songItem = AVPlayerItem(asset: anAsset)
playerItem.addObserver(self, forKeyPath: "status", options: .new, context: nil)

Ovveride observeValue :

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let status = change?[.newKey] as? Int{
            if(status == AVPlayerItemStatus.readyToPlay.rawValue){
               yourPlayer.rate = 2.0 // or whatever you want
            }
        }

    }

songItem

deinit {
        playerItem.removeObserver(self, forKeyPath: "status")
    }
+2

All Articles