我正在使用Swift创建一个iOS应用,允许用户使用youtube的rest API搜索和播放YouTube视频。为此,我使用的是UIWebView和YouTube iFrame。问题是许多视频被VEVO和某些唱片公司阻止播放,例如视频。我想知道是否有办法过滤搜索结果,以便阻止的视频不显示?我已经尝试将videoSyndicated和videoEmbeddable设置为true而没有运气。或者,内容块有没有办法?
以下是我用来搜索的代码:
var searchUrl = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=20&q=\(video)&type=video&videoEmbeddable=true&videoSyndicated=true&key=MYAPIKEY"
let url = NSURL(string: searchUrl)
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
if data != nil {
var json = JSON(data: data!)
for i in 0..<20 {
if let videoId = json["items", i, "id", "videoId"].string {
videoIds.append(videoId)
}
}
}
以下是我用来加载视频的代码:
let embededHTML = "<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='200' height='200' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>"
webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().resourceURL)