How to play video with cookies content in iOS usin

2019-03-04 11:38发布

问题:

I have used the below implementation to play a video with cookies content from the server, but it shows play icon with cross line. I have refer the link and do following implementation in swift. but I didn't get any output :(

func showVideo(url: String) {
  let videoURL = NSURL(string: url)
  var cookiesArray = [HTTPCookie]()
  guard let cookieArray = UserDefaults.standard.array(forKey: 
  Constants.Object.kCookie) as? [[HTTPCookiePropertyKey: Any]] else { 
  return }

  for cookieProperties in cookieArray {
    if let cookie = HTTPCookie(properties: cookieProperties) {
      cookiesArray.append(cookie)
    }
  } 
  let cookieArrayOptions = [AVURLAssetHTTPCookiesKey: cookiesArray]
  let assets = AVURLAsset(url: videoURL! as URL, options: cookieArrayOptions)
  let item = AVPlayerItem(asset: assets)
  videoPlayer = AVPlayer(playerItem: item)

  self.playerController.player =  self.videoPlayer 
  self.playerController.view.frame = self.view.frame
  self.present(self.playerController, animated: true, completion: nil)
  self.playerController.player?.play()
 }

Please help me on that, what is wrong in that implementation.

Thanks in advance! :)

回答1:

After going through so many ways finally I have got the solution which worked for me :

     func showVideo(url: String) {
      let videoURL = NSURL(string: url)
      let cookiesArray = HTTPCookieStorage.shared.cookies! //Stored Cookies of your request
      let values = HTTPCookie.requestHeaderFields(with: cookiesArray)// Returns a dictionary of header fields corresponding to a provided array of cookies.ex.["Cookie":"your cookies values"]
      let cookieArrayOptions = ["AVURLAssetHTTPHeaderFieldsKey": values]
      let assets = AVURLAsset(url: videoURL! as URL, options: cookieArrayOptions)
      let item = AVPlayerItem(asset: assets)
      videoPlayer = AVPlayer(playerItem: item)

      self.playerController.player =  self.videoPlayer 
      self.playerController.view.frame = self.view.frame
      self.present(self.playerController, animated: true, completion: nil)
      self.playerController.player?.play()
     }