iphone - Javascript Youtube API : buffering for ever - UIWebView iOS

标签 iphone ios uiwebview youtube youtube-api

我在 UIWebView 中使用 YouTube API。

我已经使用加载到 UIWebView 中的 HTML5 播放器创建了一个 NSString。一切都在 iPhone 5 和 iPad 上完美运行。

但是,如果我使用 iPhone 4 测试应用程序,播放器始终返回缓冲状态。只有当我明确按下播放按钮时,播放器才会开始播放,而不会再次停下来进行缓冲。看来虽然视频已经缓冲了,但是播放器给我的还是这个状态。

有人知道这个问题吗?有什么想法吗?

非常感谢您!!

最佳答案

在 LBYouTubePlayerViewController.m 文件中

将以下方法替换为旧方法....

然后测试...

      -(NSURL*)_extractYouTubeURLFromFile:(NSString *)html error:(NSError *__autoreleasing *)error {
NSString *JSONStart = nil;
// NSString *JSONStartFull = @"ls.setItem('PIGGYBACK_DATA', \")]}'";
NSString *JSONStartFull = @"bootstrap_data = \")]}'";
NSString *JSONStartShrunk = [JSONStartFull stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([html rangeOfString:JSONStartFull].location != NSNotFound)
    JSONStart = JSONStartFull;
else if ([html rangeOfString:JSONStartShrunk].location != NSNotFound)
    JSONStart = JSONStartShrunk;

if (JSONStart != nil) {
    NSScanner* scanner = [NSScanner scannerWithString:html];
    [scanner scanUpToString:JSONStart intoString:nil];
    [scanner scanString:JSONStart intoString:nil];

    NSString *JSON = nil;
    [scanner scanUpToString:@"}\";" intoString:&JSON];
    JSON = [NSString stringWithFormat:@"%@}",JSON]; // Add closing bracket } to get vallid JSON again
    // [scanner scanUpToString:@"\");" intoString:&JSON];
    JSON = [self _unescapeString:JSON];
    NSError* decodingError = nil;
    NSDictionary* JSONCode = nil;

    // First try to invoke NSJSONSerialization (Thanks Mattt Thompson)

    id NSJSONSerializationClass = NSClassFromString(@"NSJSONSerialization");
    SEL NSJSONSerializationSelector = NSSelectorFromString(@"dataWithJSONObject:options:error:");
    if (NSJSONSerializationClass && [NSJSONSerializationClass respondsToSelector:NSJSONSerializationSelector]) {
        JSONCode = [NSJSONSerialization JSONObjectWithData:[JSON dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&decodingError];
    }
    else {
        JSONCode = [JSON objectFromJSONStringWithParseOptions:JKParseOptionNone error:&decodingError];
    }

    if (decodingError) {
        // Failed

        *error = decodingError;
    }
    else {
        // Success

        NSDictionary *dict = [JSONCode objectForKey:@"content"];
        NSDictionary *dictTemp = [dict objectForKey:@"video"];
        NSArray* videos = [dictTemp objectForKey:@"fmt_stream_map"];

        NSString* streamURL = nil;
        if (videos.count) {
            NSString* streamURLKey = @"url";

            if (self.quality == LBYouTubePlayerQualityLarge) {
                streamURL = [[videos objectAtIndex:0] objectForKey:streamURLKey];
            }
            else if (self.quality == LBYouTubePlayerQualityMedium) {
                unsigned int index = MAX(0, videos.count-2);
                streamURL = [[videos objectAtIndex:index] objectForKey:streamURLKey];
            }
            else {
                streamURL = [[videos lastObject] objectForKey:streamURLKey];
            }
        }

        if (streamURL) {
            return [NSURL URLWithString:streamURL];
        }
        else {
            *error = [NSError errorWithDomain:kLBYouTubePlayerControllerErrorDomain code:2 userInfo:[NSDictionary dictionaryWithObject:@"Couldn't find the stream URL." forKey:NSLocalizedDescriptionKey]];
        }
    }
}
else {
    *error = [NSError errorWithDomain:kLBYouTubePlayerControllerErrorDomain code:3 userInfo:[NSDictionary dictionaryWithObject:@"The JSON data could not be found." forKey:NSLocalizedDescriptionKey]];
}

return nil;
}

关于iphone - Javascript Youtube API : buffering for ever - UIWebView iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17834381/

相关文章:

ios - 弹出 View Controller 未显示

ios - iPadOS:我可以请求在分屏中打开另一个应用程序吗?

ios - didSelectRowAtIndexPath 时打开新 View

javascript - 使用带有 UIWebView 的 JavaScript 通过 ID-SWIFT 检索元素

iphone - 在 ios 中启动通用应用程序时应采取的标准和注意事项

iphone - AudioSessionInitialize 使用另一个中断监听器

ios - MFMailComposeViewController 的 UINavigationBar 不一致地显示正确的图像/颜色

ios - Objective-C switch 语句的奇怪行为

ios - UIWebView 从加载的页面获取 http 状态代码

iphone - 阻止 iOS 在进入后台之前截取应用程序的屏幕截图