【问题标题】:Playing Youtube Video in iframe in UIWebView在 UIWebView 的 iframe 中播放 Youtube 视频
【发布时间】:2012-09-15 03:31:33
【问题描述】:

您好,我正在将 html 字符串解析为 Web 视图,这是我检查 youtube 视频并将它们添加到 iframe 的方法:

- (NSString *)extendYouTubeSupportInHtml:(NSString *)html {
    static dispatch_once_t onceToken;
    static NSRegularExpression *youtubeEmbedRegex;
    dispatch_once(&onceToken, ^{
        youtubeEmbedRegex = [[NSRegularExpression alloc] initWithPattern:@"<object.*src.*/v/(.*?)['|\"].*object\\s*>" options:NSRegularExpressionCaseInsensitive error:nil];
    });
    NSArray *matchs;
    if (html != nil) {

        matchs = [youtubeEmbedRegex matchesInString:html options:0 range:NSMakeRange(0, html.length)];

    }else{
        matchs = nil;
    }

    NSInteger rangeOffset = 0;
    for (NSTextCheckingResult *match in matchs) {    
        NSRange objectRange = NSMakeRange([match rangeAtIndex:0].location + rangeOffset, [match rangeAtIndex:0].length);
        NSRange idRange = NSMakeRange([match rangeAtIndex:1].location + rangeOffset, [match rangeAtIndex:1].length);
        NSString* youtubrId = [html substringWithRange:idRange];

        // Add uniq id to img tag
        NSString* iframe = [NSString stringWithFormat:@"<iframe src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" allowfullscreen></iframe>", youtubrId];
        html = [html stringByReplacingCharactersInRange:objectRange withString:iframe];

        rangeOffset += iframe.length - objectRange.length;
    }

    return html;
}

这就是我显示网络视图的方式:

- (id)initWithFrame:(CGRect)frame dict:(NSDictionary *)dict {
    self = [super initWithFrame:frame];
    if (self) {
        self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)] autorelease];
        self.webView.backgroundColor = [UIColor clearColor];
        self.webView.opaque = NO;
        self.webView.delegate = self;
        self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.webView.scalesPageToFit = NO;
        self.webView.allowsInlineMediaPlayback = YES;
        self.webView.mediaPlaybackRequiresUserAction = NO;

        [self removeBackgroundFromWebView:self.webView];      
        [self addSubview:self.webView];
        self.dictionary = dict;

    }
    return self;
}

我的问题是,当我开始播放视频时 - 内存使用量高达 30MB,视频开始卡顿,音频/视频不匹配,当我关闭它时 - 内存使用量仍然相同。

我在 AudioToolbox 中也有一些漏洞。 我正在使用 iOS 6 SDK 和 iOS 6。 iOS 6 sdk 破解了代码 - 现在它在 iOS 5 中不显示任何内容。

【问题讨论】:

    标签: iphone xcode cocoa-touch ipad uiwebview


    【解决方案1】:

    这是我在应用内播放 youtube 视频的方式。

    我正在使用 iFrame 在我的应用中加载 youtube 视频。

    按照这些步骤,你也会的。

    创建一个 uiwebview 并将其连接到您的 .h 文件。我的是 _webView。

    将此方法添加到您的 .m 文件中。

    -(void)embedYouTube{
    
        NSString *embedHTML = @"<iframe width=\"300\" height=\"250\" src=\"http://www.youtube.com/embed/rOPI5LDo7mg\" frameborder=\"0\" allowfullscreen></iframe>";
    
        NSString *html = [NSString stringWithFormat:embedHTML];
    
        [_webView loadHTMLString:html baseURL:nil];
        [self.view addSubview:_webView];
    
    }
    

    我正在使用 youtube 视频中的嵌入代码。 (我希望你知道它是什么)

    在你的 viewdidload 中调用这个方法

    [self embedYouTube];
    

    运行应用程序,您将在视图中看到视频。这种方式对我来说非常有效,我认为这对你也有帮助。

    【讨论】:

    • 我在我的旧应用中使用了同样的东西,它运行良好。但现在视频无法加载。你能看看你的应用并告诉我那里是否正常吗?
    猜你喜欢
    • 2013-04-16
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2015-12-12
    • 2015-04-11
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多