title: Check when resources are available from AVFoundation
body: <p>I'm using <code>AVFoundation</code> and creating <code>AVPlayer</code>'s but getting some errors as shown below.</p>
  
  <p>These are the 2 errors:</p>
  
  <blockquote>
    <p>2017-01-06 15:51:19.974693 DemoApp[11855:3855225] Video player Status
    Failed: player item error = Error Domain=AVFoundationErrorDomain
    Code=-11839 "Cannot Decode" UserInfo={NSUnderlyingError=0x174253ef0
    {Error Domain=NSOSStatusErrorDomain Code=-12913 "(null)"},
    NSLocalizedFailureReason=The decoder required for this media is busy.,
    NSLocalizedRecoverySuggestion=Stop any other actions that decode media
    and try again., NSLocalizedDescription=Cannot Decode}</p>
    
    <p>2017-01-06 15:51:19.974783 DemoApp[11855:3855225] Video player Status
    Failed: player error = (null)</p>
  </blockquote>
  
  <p>I'm able to print these 2 errors like so:</p>
  
  <pre><code>override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
      ///.. other code ..///
  
        case AVPlayerItemStatusFailed: {
          print("Video player Status Failed: player item error = \(self.player.currentItem.error)")
          print("Video player Status Failed: player error = \(self.player.error)")
      } 
  }
  </code></pre>
  
  <p>Apple's documentation for this error:</p>
  
  <ul>
  <li>An enum value: <code>case decoderTemporarilyUnavailable = -11839</code></li>
  <li>Link to this error: <a href="https://developer.apple.com/reference/avfoundation/averror.code/1388106-decodertemporarilyunavailable" rel="nofollow noreferrer">https://developer.apple.com/reference/avfoundation/averror.code/1388106-decodertemporarilyunavailable</a></li>
  </ul>
  
  <p>It seems Apple has a limit to how many <code>AVPlayer</code>'s the entire system could create; which means all apps share these resources. I want to check for this so that I could respond appropriately in my app. </p>
  
  <p><strong>Is there a way to check when these resources are available? Like when is the decoder available?</strong> </p>
  
  <h2>Update 1</h2>
  
  <p>Below is snippet of what I'm doing. On an iPhone 6s, there seems to be a limit of 16 <code>AVPlayer</code>'s that can be created and played simultaneously. In this app, the function below called <code>refreshVideoPlayersAndTable</code> creates 10 <code>AVPlayer</code>'s every time the app launches and starts playing all of them. When the app goes to background, I release these shared resources by deleting and removing all video players. </p>
  
  <p>Note: This error occurs mostly if I use another app that plays many videos simultaneously and is therefore using the shared resources from iOS. There aren't too many apps that use 10 or more <code>AVPlayer</code>'s at once but there are a couple. When I use another app that does so and then switch to my app right away, then I need to put the <code>delay</code> like below to avoid any decode error's.   </p>
  
  <p><strong>My main goal is to check for when the resources are available instead of using an arbitrary <code>delay</code> in order to create the video players.</strong></p>
  
  <pre><code>class MyViewController: UIViewController {
  
      override func viewDidLoad() {
              super.viewDidLoad()
  
          registerNotifications()
      }
  
      func applicationWillEnterForeground(notification: Notification) {
  
          /// Hack: Without a delay, it seems that iOS does not have the resources
          /// available to refresh the AVPlayer's. We get `decode errors` if we
          /// instantiate them without a delay.
          delay(1) { [weak self] in
            self?.refreshVideoPlayersAndTable()
          }
      }
  
      func applicationDidEnterBackground(notification: Notification) {
        /// Release shared resources - delete and remove all AVPlayer's
          removeAllVideoPlayers()
      }
  
      func registerNotifications() {
          NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.applicationDidEnterBackground), name: .UIApplicationDidEnterBackground, object: nil)
              NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.applicationWillEnterForeground), name: .UIApplicationWillEnterForeground, object: nil)
      }
  }
  </code></pre>