How to play video with cookies content in iOS using AVPlayer in Swift?

二次信任 提交于 2019-12-20 06:48:41

问题


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()
     }


来源:https://stackoverflow.com/questions/48899128/how-to-play-video-with-cookies-content-in-ios-using-avplayer-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!