2014-09-03 25 views
0

在該方法中:XCDYouTubeVideoPlayerViewController videoURL來作爲零

- (void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
NSError *error = nil; 
NSURL *videoURL = [self videoURLWithData:self.connectionData error:&error]; 
if (videoURL) 
    self.moviePlayer.contentURL = videoURL; 
else if (self.elFields.count > 0) 
    [self startVideoInfoRequest]; 
else 
    [self finishWithError:error]; 
} 

videoURL返回爲零,因此,它是將差錯塊。我使用的Youtube視頻ID是「5Uls9v1nnss」。什麼似乎是這個問題?

這是用來檢索videoURL的videoURLWithData方法是這樣的:

- (NSURL *) videoURLWithData:(NSData *)data error:(NSError * __autoreleasing *)error 
{ 
NSString *videoQuery = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
NSStringEncoding queryEncoding = NSUTF8StringEncoding; 
NSDictionary *video = DictionaryWithQueryString(videoQuery, queryEncoding); 
NSMutableArray *streamQueries = [[video[@"url_encoded_fmt_stream_map"] componentsSeparatedByString:@","] mutableCopy]; 
[streamQueries addObjectsFromArray:[video[@"adaptive_fmts"] componentsSeparatedByString:@","]]; 

NSMutableDictionary *streamURLs = [NSMutableDictionary new]; 
for (NSString *streamQuery in streamQueries) 
{ 
    NSDictionary *stream = DictionaryWithQueryString(streamQuery, queryEncoding); 
    NSString *type = stream[@"type"]; 
    NSString *urlString = stream[@"url"]; 
    if (urlString && [AVURLAsset isPlayableExtendedMIMEType:type]) 
    { 
     NSURL *streamURL = [NSURL URLWithString:urlString]; 
     NSString *signature = stream[@"sig"]; 
     if (signature) 
      streamURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@&signature=%@", urlString, signature]]; 

     if ([[DictionaryWithQueryString(streamURL.query, queryEncoding) allKeys] containsObject:@"signature"]) 
      streamURLs[@([stream[@"itag"] integerValue])] = streamURL; 
    } 
} 

for (NSNumber *videoQuality in self.preferredVideoQualities) 
{ 
    NSURL *streamURL = streamURLs[videoQuality]; 
    if (streamURL) 
    { 
     NSString *title = video[@"title"]; 
     NSString *thumbnailSmall = video[@"thumbnail_url"]; 
     NSString *thumbnailMedium = video[@"iurlsd"]; 
     NSString *thumbnailLarge = video[@"iurlmaxres"]; 
     NSMutableDictionary *userInfo = [NSMutableDictionary new]; 
     if (title) 
      userInfo[XCDMetadataKeyTitle] = title; 
     if (thumbnailSmall) 
      userInfo[XCDMetadataKeySmallThumbnailURL] = [NSURL URLWithString:thumbnailSmall]; 
     if (thumbnailMedium) 
      userInfo[XCDMetadataKeyMediumThumbnailURL] = [NSURL URLWithString:thumbnailMedium]; 
     if (thumbnailLarge) 
      userInfo[XCDMetadataKeyLargeThumbnailURL] = [NSURL URLWithString:thumbnailLarge]; 

     [[NSNotificationCenter defaultCenter] postNotificationName:XCDYouTubeVideoPlayerViewControllerDidReceiveMetadataNotification object:self userInfo:userInfo]; 
     return streamURL; 
    } 
} 

if (error) 
{ 
    NSMutableDictionary *userInfo = [@{ NSURLErrorKey: self.connection.originalRequest.URL } mutableCopy]; 
    NSString *reason = video[@"reason"]; 
    if (reason) 
    { 
     reason = [reason stringByReplacingOccurrencesOfString:@"<br\\s*/?>" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, reason.length)]; 
     NSRange range; 
     while ((range = [reason rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound) 
      reason = [reason stringByReplacingCharactersInRange:range withString:@""]; 

     userInfo[NSLocalizedDescriptionKey] = reason; 
    } 

    NSInteger code = [video[@"errorcode"] integerValue]; 
    *error = [NSError errorWithDomain:XCDYouTubeVideoErrorDomain code:code userInfo:userInfo]; 
} 

return nil; 
} 
+0

只適用於一些videos.Hence,關閉它。 – 2014-09-03 06:57:05

+0

這種問題應該在項目的[問題頁面]上報告(https://github.com/0xced/XCDYouTubeKit/issues)。 – 0xced 2014-09-03 07:19:11

回答

0

您應該使用的XCDYouTubeKit最新版本(2.0.2寫作的),XCDYouTubeVideoPlayerViewController的繼任者。視頻5Uls9v1nnss應該可以玩。