@H_403_0@1:youtube player可以通过id 或url来播放视频
@H_403_0@主要用到的两个方法是 loadWithVideoId 和 loadVideoByURL
@H_403_0@这两者的区别是 loadVideoByURL加载完之后会自动播放的
@H_403_0@loadWithVideoId不会自动播放 加载完之后会显示第一政的图和一个播放按钮
@H_403_0@
@H_403_0@2:通过url获取id的重点实现方法如下:
@H_403_0@private extension NSURL {
func queryStringComponents() -> [String: AnyObject] {
var dict = [String: AnyObject]()
// Check for query string
if let query = self.query {
// Loop through pairings (separated by &)
for pair in query.componentsSeparatedByString("&") {
// Pull key,val from from pair parts (separated by =) and set dict[key] = value
let components = pair.componentsSeparatedByString("=")
if (components.count > 1) {
dict[components[0]] = components[1]
}
}
}
return dict
}
}
@H_403_0@
@H_403_0@
@H_403_0@public func videoIDFromYouTubeURL(videoURL: NSURL) -> String? { if let host = videoURL.host,pathComponents = videoURL.pathComponents where pathComponents.count > 1 && host.hasSuffix("youtu.be") { return pathComponents[1] } return videoURL.queryStringComponents()["v"] as? String } 原文链接:https://www.f2er.com/swift/323071.html