问题
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()
}
来源:https://stackoverflow.com/questions/43618851/how-to-embed-youtube-or-facebook-live-streaming-in-my-ios-app