在这种方法中:

- (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返回nil,因此将进入错误块。我正在使用的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;
}

最佳答案

您应该使用XCDYouTubeKit的最新版本(截至撰写时为2.0.2),它是XCDYouTubeVideoPlayerViewController的后继版本。视频5Uls9v1nnss应该可以正常播放。

10-04 20:24