Skip to main content
Skip table of contents

Observing player errors

Observing player errors using the OTVAVFoundationErrors notification

When OTVAVPlayer and OTVAVPlayerItem encounter an error when initiating or playing content, it produces a notification using the OTVAVFoundationErrors notification. 

CODE
/// Notification when OTVAVPlayer or OTVAVPlayerItem encounters an error.
/// which gives the statusCode, domain and message for the error encounted. 
public let OTVAVFoundationErrors = Notification.Name("otvAVFoundationErrors")

Two types of OTVAVFoundationErrors are encountered: OTVAVPlayer and OTVAVPlayerItem.

CODE
/**
 `OTVAVFoundationError` is an enum that represent they type of OTVAfoundation type either, OTVAVPlayer or OTVAVPlayerItem
 */
public enum OTVAVFoundationError: Int {
  // AVPlayerError
  case otvAVPlayer = 1101
  // AVPlayerItemError
  case otvAVPlayerItem = 1102
}

Listening to OTVAVFoundationErrors can be done by first registering to listen to the notification. 

CODE
NotificationCenter.default.addObserver(self, selector: #selector(opyPlayerErrorsReturned), name: OTVAVFoundationErrors, object: nil)

The user filters on the type of OTVAVFoundationError returned (either OTVAVPlayer or OTVAVPlayerItem), then uses the information returned from the userInfo object of the notification. The userInfo object is a dictionary containing three keys, statusCode, domain and message. 

CODE
@objc func opyPlayerErrorsReturned(_ notification: NSNotification) {
  if let error = notification.object as? OTVAVFoundationError, let userInfo = notification.userInfo {
      let statusCode = userInfo["statusCode"]
      let domain = userInfo["domain"]
     let messsage = userInfo["message"]
      switch error {
            case .otvAVPlayer:
                  print("OTVAVPlayer errror:: Status code = ", statusCode, " domain = ", domain, " message = ", messsage)
            case .otvAVPlayerItem:
                  print("OTVAVPlayerItem errror:: Status code = ", statusCode, " domain = ", domain, " message = ", messsage)
            @unknown default:
                    NSLog("Unexpected OPY error: \(error)")
            }
      }
}

In the case of otvAVPlayerItem errors, the errors returned are that of AVPlayerItem error codes returned during playback when the item fails to play, or the item status changes to failed state. 

Observing player errors using the AVPlayerItemNewErrorLogEntry notification

To listen to AVPlayerItemNewErrorLogEntry for the content being played, create a notification observer on AVPlayerItemNewErrorLogEntry using the OTVAVPlayerItem of the content being played.

CODE
NotificationCenter.default.addObserver(self, selector: #selector(handleAVPlayerItemErrorLog),
                                           name: NSNotification.Name.AVPlayerItemNewErrorLogEntry,
                                           object: self.playerItem)

This notification is triggered every time the player item encounters a media error. You can filter on the last error log event using the code below.

CODE
 private func latestPlayerItemErrorLogEvent() -> AVPlayerItemErrorLogEvent? {
    guard let item = playerItem, let errorLog = item.errorLog() else {
      return nil
    }
    return errorLog.events.last
  }

An example of how to handle the information returned from the AVPlayerItemNewErrorLogEntry notification. All errors returned through this notification are from AVFoundation only and do not contain any OPYPlayback errors.

CODE
  @objc func handleAVPlayerItemErrorLog(notification: Notification) {
    if let statusCode = latestPlayerItemErrorLogEvent()?.errorStatusCode, let message = latestPlayerItemErrorLogEvent()?.errorComment,  let domain = latestPlayerItemErrorLogEvent()?.errorDomain {
      print("The status code is: ", statusCode, " the error domain is: ", domain, " the error message is: ", message)
    }
  }


JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.