Observing values on AVPlayerItem in iOS9

2019-03-22 00:20发布

问题:

I have an app that uses AVPlayer to play an AVPlayerItem (video) from a remote URL. In iOS 6-8 I have been observing the AVPlayerItem's value for loadedTimeRanges to notify me when the playerItem is ready to be played by the player. This also works when observing the value for the item's duration, I believe.

After updating to iOS 9 beta, none of the values on AVPlayerItem I observe ever makes it to the observeValueForKeyPath-method. Just as if I'm not observing them at all. I am still being notified by the values on AVPlayer, but not on AVPlayerItem. Could this be a bug, or did something change in the environment here? I can't find anything about this..

For clarification, in iOS 6-8 the videos are starting to play as soon as there are any loaded time ranges. In iOS9 I am never notified when any time ranges have been loaded.

Update

After observing the value status for AVPlayerItem I have now confirmed that the item's status is changed to Failed. By logging out the item's NSError after failing, I get this:

Error Domain=AVFoundationErrorDomain Code=-11800 
    "The operation could not be completed" 
    UserInfo=0x146023c90 {NSUnderlyingError=0x144f547d0 
    "The operation couldn’t be completed. (OSStatus error -1022.)",
    NSLocalizedFailureReason=An unknown error occurred (-1022), 
    NSLocalizedDescription=The operation could not be completed}

回答1:

I ran into the same issue today. In my case, loading the video was failing due to the new App Transport Security feature in iOS 9.

You can add a per-domain exception to your info.plist like this:

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>yourserver.com</key>
    <dict>
      <!--Include to allow subdomains-->
      <key>NSIncludesSubdomains</key>
      <true/>
      <!--Include to allow HTTP requests-->
      <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
      <true/>
      <!--Include to specify minimum TLS version-->
      <key>NSTemporaryExceptionMinimumTLSVersion</key>
      <string>TLSv1.1</string>
    </dict>
  </dict>
</dict>

If you need to load videos from arbitrary domains, you can disable App Transport Security altogether, although this is not recommended.

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>