how to embed youtube or facebook live streaming in

2019-04-02 14:22发布

I have been looking over facebook SDK for IOS but I could not really get a solid answer . So I am asking here , weather is it possible to stream the live facebook video or youtube video in my IOS app using swift ? if yes , then feel free to give me some tips or share good reads and references Tnx

1条回答
Root(大扎)
2楼-- · 2019-04-02 15:01

To embed youtube live stream onto your iOS app use the following:

   @IBOutlet weak var LiveStreamViewer: UIWebView!
   let channelID = "[channelID]"

   func loadLiveStream {
        guard
            let youtubeURL = URL(string: "https://www.youtube.com/embed?
                live=1&channel=\(channelID)")
            else { return }
        LiveStreamViewer.loadRequest( URLRequest(url: youtubeURL) )
   }

However, using this will bring up an error within the WebView if you are not actually streaming, and someone tries to play the video. To plan for that add something similar to the following function:

   let googleAPI = "[youtubeAPIKey]"


   func checkLive() {
       let liveChannel = URL(string: "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=\(channelID)&type=video&eventType=live&key=\(googleAPI)")
       URLSession.shared.dataTask(with:liveChannel!, completionHandler: {(data, response, error) in
           guard let data = data, error == nil else { return }

           do {
               let parsedData = try JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]
               let pageInfo = parsedData["pageInfo"] as! [String:Any]

               print(pageInfo)

               let currentState = pageInfo["totalResults"] as! Int
               print(currentState)

               if currentState == 0 {
                   print("No Stream Right Now")
                   self.LiveStreamViewer.isHidden = true
                   //DO SOMETHING TO LET THEM KNOW YOU ARENT STREAMING
               } else {
                   print("Streaming")
                   self.LiveStreamViewer.isHidden = false
                   self.loadLiveStream()
               }

           } catch let error as NSError {
               print(error)
           }
       }).resume()
   }
查看更多
登录 后发表回答