YouTube APIを使用してYouTubeチャンネルのアプリを作成しています。私のビデオにはmaxresサムネイルがありますが、その中にはサムネイルがありません。私が表示しているテーブルビューにビデオが表示され、maxresサムネイルがない場合、アプリケーションがクラッシュします。私はそのビデオのためのmaxresサムネイルがある場合、maxresサムネイルを使用するようにAPIに指示したいが、そうでなければ、高いものを使用する。これは私のコードです。YouTube APIを使用してMaxresまたは高サムネイルを表示
Alamofire.request("https://www.googleapis.com/youtube/v3/search", parameters: ["part":"snippet","maxResults":50,"channelId":CHANNEL_ID,"playlistId":UPLOADS_PLAYLIST_ID,"q":searchText,"type":"video","key":API_KEY], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
if let JSON = response.result.value {
if let dictionary = JSON as? [String: Any] {
var arrayOfVideos = [Video]()
guard let items = dictionary["items"] as? NSArray else { return }
for items in dictionary["items"] as! NSArray {
print("//Printing Video\n\(items)")
// Create video objects off of the JSON response
let videoObj = Video()
videoObj.videoID = (items as AnyObject).value(forKeyPath: "id.videoId") as! String
videoObj.videoTitle = (items as AnyObject).value(forKeyPath: "snippet.title") as! String
videoObj.videoDescription = (items as AnyObject).value(forKeyPath: "snippet.description") as! String
videoObj.videoThumbnailUrl = (items as AnyObject).value(forKeyPath: "snippet.thumbnails.maxres.url") as! String // Here I need to tell the API to use the maxres thumbnail if there is one.
arrayOfVideos.append(videoObj)
}
maxresサムネイルを探していないようです。高、中、デフォルトのみを表示します。しかし、私がplaylistItemsを使うときはいつでも、それはmaxresサムネイルを表示します。うーん...
申し訳ありませんが、動作しませんでした。私は 'videoObj.videoThumbnailUrl =(AnyObjectとしてのアイテム).value(forKeyPath:" snippet.thumbnails.maxres.url ")を!文字列 'を指定したコードで置き換えます。これは、maxresを持つ動画のデフォルトサムネイルを表示しているだけです。 APIがmaxresを呼び出さないようです。端末では、高、中、デフォルトのみが表示されます。しかし、私がplaylistItemsを使用しているときはいつでも、maxresオプションがあります。うーん... –