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