【问题标题】:Javascript Youtube API: buffering for ever - UIWebView iOSJavascript Youtube API:永远缓冲 - UIWebView iOS
【发布时间】:2013-07-23 22:49:11
【问题描述】:

我在 UIWebView 中使用 YouTube API。

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

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

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

非常感谢您!

【问题讨论】:

    标签: iphone ios uiwebview youtube youtube-api


    【解决方案1】:

    在 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;
    }
    

    【讨论】:

    • 我不使用 LBYouTubePlayerView。 UIWebView 在应用程序中使用。无论如何感谢您的回复!
    • 我遇到了同样的问题,兄弟..但是..我使用了 LBYoutubPlayer...github.com/larcus94/LBYouTubeView/commit/… 检查它,我希望你会遇到问题..可能是..
    猜你喜欢
    • 2012-10-28
    • 2021-05-15
    • 1970-01-01
    • 2013-03-07
    • 2015-04-12
    • 2013-02-03
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多