Swift iOS -How to Access Error from AVPlayerItemStatus?
Asked Answered
D

1

8

I'm using an AVPlayer to play a video url. I followed Apple's code to play the video and handle errors:

Inside the section that says Respond to A State Change there is a switch statement that has a .failed case and the commented out code says: // Player item failed. See error. I've had this run a couple of times by putting a break point there.

enter image description here

The problem is I don't see anything like a variable with a NSError type that would give me an option to actually print out what the error is. How can I find out what the .failed the error is?

Observe the Player's State:

let url: URL = // Asset URL
var asset: AVAsset!
var player: AVPlayer!
var playerItem: AVPlayerItem!

// Key-value observing context
private var playerItemContext = 0

let requiredAssetKeys = [
    "playable",
    "hasProtectedContent"
]

func prepareToPlay() {
    // Create the asset to play
    asset = AVAsset(url: url)

    // Create a new AVPlayerItem with the asset and an
    // array of asset keys to be automatically loaded
    playerItem = AVPlayerItem(asset: asset,
                              automaticallyLoadedAssetKeys: requiredAssetKeys)

    // Register as an observer of the player item's status property
    playerItem.addObserver(self,
                           forKeyPath: #keyPath(AVPlayerItem.status),
                           options: [.old, .new],
                           context: &playerItemContext)

    // Associate the player item with the player
    player = AVPlayer(playerItem: playerItem)
}

Respond to a State Change

override func observeValue(forKeyPath keyPath: String?,
                           of object: Any?,
                           change: [NSKeyValueChangeKey : Any]?,
                           context: UnsafeMutableRawPointer?) {

    // Only handle observations for the playerItemContext
    guard context == &playerItemContext else {
        super.observeValue(forKeyPath: keyPath,
                           of: object,
                           change: change,
                           context: context)
        return
    }

    if keyPath == #keyPath(AVPlayerItem.status) {
        let status: AVPlayerItemStatus
        if let statusNumber = change?[.newKey] as? NSNumber {
            status = AVPlayerItemStatus(rawValue: statusNumber.intValue)!
        } else {
            status = .unknown
        }

        // Switch over status value
        switch status {
        case .readyToPlay:
            // Player item is ready to play.
        case .failed:
            // Player item failed. See error.
        case .unknown:
            // Player item is not yet ready.
        }
    }
}
Dissident answered 13/3, 2018 at 3:39 Comment(0)
H
9

if status is .failed then you can access the error from AVPlayer itself like this.

    switch status {
    case .readyToPlay:
        // Player item is ready to play.
    case .failed:
        handleErrorWithMessage(player.currentItem?.error?.localizedDescription, error:player.currentItem?.error)
    case .unknown:
        // Player item is not yet ready.
    }

Find below function.

func handleErrorWithMessage(_ message: String?, error: Error? = nil) {
    print("Error occured with message: \(message), error: \(error).")
}

Hope this helps.

Hurricane answered 13/3, 2018 at 5:27 Comment(4)
Thanks I’ll let you know how it works out by tomorrow and get back to youDissident
@LanceSamaria It's my target, can you please check & at least upvoteHurricane
@LanceSamaria checked?Hurricane
Thanks again for the help!Dissident

© 2022 - 2024 — McMap. All rights reserved.