Hello I'm parsing html string into web view and here is how I check for youtube videos and add them to 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;
}
And here is how I'm displaying the web view:
- (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;
}
My issue is that when I start playing video - memory usage is going up to 30MB, video starts to stutter, audio/video doesn't match, and when i close it - the memory usage is still the same.
I have a couple of leaks in AudioToolbox, also. I'm using iOS 6 SDK, and iOS 6. iOS 6 sdk broke the code - now it's not displaying anything in iOS 5.
This is my way of play a youtube video inside a app.
I am using iFrame to load youtube video inside my app.
follow this steps and you will too.
create a uiwebview and connect it to your .h file. Mine is _webView.
Add this method to your .m file.
-(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];
}
I am using the embedded code in youtube video. (I hope you know what it is)
call this method inside your viewdidload
[self embedYouTube];
Run the app and you will see the video in your view. This way is perfectly working for me and i think this will help for your too.