ios - XCDYouTubeVideoPlayerViewController videoURL为nil

标签 ios objective-c youtube youtube-api youtube-data-api

在这种方法中:

- (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应该可以正常播放。

关于ios - XCDYouTubeVideoPlayerViewController videoURL为nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25637306/

相关文章:

android - 在Android中缩放/最小化YoutubePlayerView时调整视频大小

javascript - 关于在点击事件触发时停止 YouTube 视频的建议

ios - 通过 reloadData 更新 UITableView 时保持选择

ios - cmd 行测试因 SSL 证书错误而失败

objective-c - NSNumberFormatter 四舍五入为负零

c++ - 从 C/C++ 调用时,Objective C stdin/stdout 管道如何工作?

javascript - 加载多个视频而不播放

iPad MPMoviePlayerController - 禁用全屏

ios - .onAppear 是否导致文本在 SwiftUI 中消失?

ios - 当出现调用或热点状态栏时,我的应用程序布局被破坏