2017-09-14 2 views
1

私は&を読んでおり、ビデオアセットの読み込みに関するAppleのサンプルコードを試しています。AVURLAsset loadValuesAsynchronously never fail

AVURLAssetloadValuesAsynchronouslyの機能は、機上で飛行機モードをオンにしても、またはMacでWiFiをオフにしても失敗することはありません。誰かがloadValuesAsynchronouslyがどのような状況でキーを読み込めないのか知っていますか?

これは相対sample codeです:

asset.loadValuesAsynchronously(forKeys: PlayerViewController.assetKeysRequiredToPlay) { 

    /* 
     The asset invokes its completion handler on an arbitrary queue. 
     To avoid multiple threads using our internal state at the same time 
     we'll elect to use the main thread at all times, let's dispatch 
     our handler to the main queue. 
    */ 
    DispatchQueue.main.async() { 
     /* 
      This method is called when the `AVAsset` for our URL has 
      completed the loading of the values of the specified array 
      of keys. 
     */ 

     /* 
      Test whether the values of each of the keys we need have been 
      successfully loaded. 
     */ 
     for key in PlayerViewController.assetKeysRequiredToPlay { 
      var error: NSError? 

      if asset.statusOfValue(forKey: key, error: &error) == .failed { 
       let stringFormat = NSLocalizedString("error.asset_%@_key_%@_failed.description", comment: "Can't use this AVAsset because one of it's keys failed to load") 

       let message = String.localizedStringWithFormat(stringFormat, title, key) 

       self.handleError(with: message, error: error) 

       return 
      } 
     } 

     // We can't play this asset. 
     if !asset.isPlayable || asset.hasProtectedContent { 
      let stringFormat = NSLocalizedString("error.asset_%@_not_playable.description", comment: "Can't use this AVAsset because it isn't playable or has protected content") 

      let message = String.localizedStringWithFormat(stringFormat, title) 

      self.handleError(with: message) 

      return 
     } 

     /* 
      We can play this asset. Create a new AVPlayerItem and make it 
      our player's current item. 
     */ 
     self.loadedAssets[title] = asset 

     let name = (thumbnailResourceName as NSString).deletingPathExtension 
     let type = (thumbnailResourceName as NSString).pathExtension 
     let path = Bundle.main.path(forResource: name, ofType: type)! 

     let thumbnail = UIImage(contentsOfFile: path)! 

     self.assetTitlesAndThumbnails[asset.url] = (title, thumbnail) 
    } 
} 

答えて

1

は最後に自分の質問に答えるために、これを考え出した:I HLSのビデオを使用してきたとURLの形式https://www.foo.com/master.m3u8です。 loadValuesAsynchronouslyは、URLに有効なHLSファイルが含まれているかどうかを判断するネットワーク操作は行いません。 「再生可能」キーが常に有効である理由です:

static let assetKeysRequiredToPlay = [ 
    "playable", 
    "hasProtectedContent" 
] 

私はまた、このような「期間」と「トラック」として、いくつかの再生情報を追加する場合、任意のネットワーク接続が存在しない場合、それは失敗しますが、けれども。詳細は、この回答を参照してください:https://stackoverflow.com/a/31418812/1035008