Youtube Video crash app iOS (Received memory warning) - iphone

I am trying to play youtube videos in my app..
and it works fine.. but total are 7 in numbers and when I play one by one all the videos. and plays the fifth video. It crash my app with memory waring
2013-03-07 12:31:01.167 Magazine[3015:707] Received memory warning.
why this error is coming on particular video.. is this because of the memory low or some other reason.I have embedded the youtube in my app . please check the code.
why this memory issue is coming for the particular youtube video.
I think memory get full when I play videos on my iPad1 ..how to release it from memory...
UIWebView *youtube_player=[[UIWebView alloc]initWithFrame:CGRectMake(20, Yposition +k*Height, 400, Height)];
youtube_player.backgroundColor = [UIColor blackColor];
youtube_player.opaque = NO;
youtube_player.delegate=self;
NSString *videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"100%%\" height=\"240px\" src=\"%#\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>", [[videos objectAtIndex:k] _video_path]];
youtube_player.scrollView.scrollEnabled=NO;
UIScrollView *scrollView = [youtube_player.subviews objectAtIndex:0];
scrollView.delegate = self;
[youtube_player loadHTMLString:videoHTML baseURL:nil];
youtube_player.layer.borderWidth=1.0f;
[mMoviePlayersArray_por addObject:youtube_player];

Related

Youtube video in UIWebview not working

Using this code to play YouTube video in UIWebView
- (void)embedYouTube {
UIWebView *webView = [[[UIWebView alloc] initWithFrame:self.view.bounds] autorelease];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
// iframe
videoHTML = [NSString stringWithFormat:#"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:50%%; margin-top:-130px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<iframe width=\"560%%\" height=\"315px\" src=\"%#\"http://www.youtube.com/embed/j9CukQje2qw\" frameborder=\"0\" allowfullscreen></iframe>\
</body>\
</html>", videoURL];
[videoView loadHTMLString:videoHTML baseURL:nil];
}
When i click on this LaunchVideo UIButton all it shows black screen on iOS device
-(void)LaunchVideo:(id)sender
{
self.videoURL = #"http://www.youtube.com/embed/j9CukQje2qw";
WebViewController *webViewController = [[[WebViewController alloc] initWithNibName:nil bundle:nil] autorelease];
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
webViewController.videoURL = self.videoURL;
[self presentModalViewController:webViewController animated:YES];
}
If anyone can point out what i m missing or doing wrong in this code. Why youtube video is not showing. Not using interface builder doing programmtically. Got this project code from web. Tried this project code on ios device it works fine for that project but when applied its code in my project then it is not working. Only difference that his projectcode from web uses interface builder and i am doing it programmtically.I dont get it.
Please help what is wrong with this code.
Thanks for help.
Here is another way to load the youtube video into your app. First, put a UIWebView into your interface and size it to how big you want the video should show (there will be a button to make it full screen), then make an IBOutlet to it, so you can change its properties of hidden (to make it pop up and go away) Here is some code. In the code there is a method used called webviewshow, this calls a simple method that basically just says
webview.hidden = NO;
webviewbutton.hidden = NO;
Then there is a button that also pops up that makes the web view go away when tapped. Here is the code to load the youtube video into the web view
// Create your query ...
NSString* searchQuery = [NSString stringWithFormat:#"http://www.youtube.com/embed/j9CukQje2qw];
// Be careful to always URL encode things like spaces and other symbols that aren't URL friendly
searchQuery = [searchQuery stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
// Now create the URL string ...
NSString* urlString = searchQuery;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView loadRequest:request]; currentTimer= [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(webviewshow) userInfo:nil repeats:NO];;
It waits 2 seconds (usual wait time for most connections) until the web view is shown, so the page is loaded and the user does not have to watch it load.
Then when you want to make the video go away, make a method to close the web view like so
- (void) makewebviewGoAway {webview.hidden = YES; webviewbutton.hidden = YES;}
Hopefully, that was helpful. IF you need further help or explanation, just comment off of this.

Youtube video causes Main View to open afterwards.

I just implemented a youtube video view on my app, but when I close the youtube video after watching it.. it wont close. It opens the main view controller then places it above all other view controllers (i think). heres how I am showing the youtube video:
Inside MyViewController.m:
NSString *lessonHTML = #"http://www.youtube.com/watch?v=0P7UgKjzndo";
NSString *ytHtml = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:ytHtml, lessonHTML, youtubeView.frame.size.width, youtubeView.frame.size.height];
NSLog(#"HTML String: %#",html);
[youtubeView loadHTMLString:html baseURL:nil];
when the thumbnail is pressed it loads the video. Then when I click 'done' it bring me back to my main ViewController not the one I was in.
Im using Storyboards just incase its something to do with that.
Beer and upvotes for epic answers thanks!

Can't play video using webview

I am using webview to show the video thumb in my application, all is working fine and I can see the video thumb there in my application. But when I tap on the webview It shows loading the video but movie player never shown in my application and status bar disappears. but when I use same code in another sample application, it plays the video successfully. I am not able to find the issue. Here is my code segment
NSString *vimeoURL = [NSString stringWithFormat:#"http://player.vimeo.com/video/%#?title=0&byline=0&portrait=0", [stepDic objectForKey:video_id]];
NSString *htmlStr = [NSString stringWithFormat:#"<iframe src=\"%#\" width=\"100\" height=\"100\" frameborder=\"0\" webkitAllowFullScreen allowFullScreen></iframe>", vimeoURL];
[aWebView loadHTMLString:htmlStr baseURL:nil];
[self.view addSubview:aWebView];

How to start an embedded YouTube video at a certain timestamp in iphone

I want to start an embedded youtube video from a timestamp. I know how to do it on browsers but that does not seem to work on iPhone. Here is a detailed tutorial by MATT CUTTS.
Start an embedded video at a certain timestamp
In above link you can see if we append #t=31m08s or &start=200 in url it works but when i use same url inside UiWebView, default youtube player starts video from first frame. Can anyone have an idea or have implemented same. I am using UIWebView with embed tag to play youtube video. I am using below embed tag inside UIWebView.
<embed id="yt" src="http://www.youtube.com/watch?v=dsFQ9kM1qDs" type="application/x-shockwave-flash" width= "500" height="400"></embed>
Below is the code i am using to play this video in UIWebView
- (IBAction) playYoutube {
[self embedYouTube:#"http://www.youtube.com/watch?v=dsFQ9kM1qDs#t=2m08s"];
}
- (void)embedYouTube:(NSString*)url
{
[webView setFrame:CGRectMake(0, 0, 480, 320)];
NSString* embedHTML = #"\
<html><head>"
"<style type=\"text/css\">"
"body {"
"background-color: transparent;"
"color: white;"
"} </style>"
"</head><body style=\"margin:0\">"
" <embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" "
"width=\"%0.0f\" height=\"%0.0f\"></embed>"
" </body></html>";
NSString* html = [NSString stringWithFormat:embedHTML, url, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
}
According to the Youtube API team there no way to do this with iPhone/iPad etc. - see https://groups.google.com/group/youtube-api-gdata/browse_thread/thread/ef4f434347622448/0f65f465d671d74c
Try adding ?t=1m10s at the end of the link
E.g. src="https://youtu.be/dsFQ9kM1qDs?t=1m10s"
Note: Make sure that you use youtu.be link (the link that you get when you click share button on YouTube) and not youtube.com link

How to play videos from youtube, metacafe etc in a native iphone app?

I get a feed which consists of a URL pointing to the videos from sites like youtube, metacafe etc.
I found this post suggesting how to use UIWebView to load the video into the app. However I don't want the video to play in full screen mode. I want it to play in the frame I have allocated for it.
How do I make the youtube video which is loaded by embedding html into the uiwebview play in the frame allocated to it and not in full screen mode?
How do we play videos from sites like metacafe and comedy central and other sites where url doesn't point to a file but instead loads a player if pasted on browser.
Are there any applications which have achieved to play the same as a native iphone app?
Any help in this regards would be greatly helpful.
EDIT:
I want to know how do we play a video from the url say - http://www.dailymotion.com/swf/video/xe1l96
I would like to know how to play the video in the webview itself. I want to use the space around it to display other information. Currently it forces full screen mode.
Safari is able to play dailymotion and vimeo videos by lauching the quicktime player. So I would like to know how to redirect to play the videos using quicktime player.
How do I play videos from youtube and metacafec in a native iphone app?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UIWebView *wbView = (UIWebView *)[cell.contentView viewWithTag:1];
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML,url, 64.0, 64.0];
[wbView loadHTMLString:html baseURL:nil];
}
"url" is the youtube video url
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [category_table cellForRowAtIndexPath:indexPath];
UIWebView *wbView = (UIWebView *)[cell.contentView viewWithTag:1];
UIButton *btn = [self findButtonInView:wbView];
[btn sendActionsForControlEvents:UIControlEventTouchUpInside];
}
// detecting the play button on the you tube video thumbnail
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
UIButton *button = [self findButtonInView:_webView];
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}
Basically, i had to show thumbnails in table cell
Praveen,
Your video will always play full screen on iPhone & iPod. The mentioned flag (allowsInlineMediaPlayback) only works on iPad...
YouTube is easy. Just set the UIWebView's allowsInlineMediaPlayback property to YES.
The other stuff is just a flash player that wraps around a video. It's going to be harder, and probably not worth it without the backing of the site in question.
Good luck, and if you need any more help just comment/edit your question.