Making youtube video to autoplay - iphone

I need help. I have been trying this for days now. I need to integrate a Youtube video to my iPhone application (without exiting the application). The thing is that i know how to embed the video. but i need to autorun the video without showing the thumbnail.
My code shows a thumbnail view, and after i click it the video starts to play. But what i need is to play the video automatically (without displaying the thumbnail view first).
I have seen many application that has done this, but i am unable to implement it in my application.
Here's my code, can you tell me what is wrong, and how to fix it.
I have also included the parameter autoplay still its not working.
note: I have already seen apps that has this feature :)
NSString *html = #"<html><head> <meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head> <body style=\"background:#F00;margin-top:0px;margin-left:0px\"> <param name=\"movie\" value=\"http://www.youtube.com/embed/VO6FQ62Vip4\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"autoplay\" value=\"1\"></param><embed src=\"http://www.youtube.com/embed/VO6FQ62Vip4\" type=\"application/x-shockwave-flash\" width=\"560\" height=\"315\" allowfullscreen=\"true\" autoplay=1></embed></body></html>";
[webView loadHTMLString:html baseURL:[NSURL URLWithString:#"http://www.youtube.com/embed/VO6FQ62Vip4"]];
[self.view addSubview:webView];

you need to find the play button that is inside the UIWebView and send it UIEventTouchUpInside action.

So my html looked like
<div class="video-autoplay">
<iframe width="560" height="315" src="https://www.youtube.com/embed/wqFLcE923fc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
and I made jQuery code to add the variables that are required to autoplay youtube video so, once user paste the youtube embed code, it right away makes the video autoplay.
const iframeVideo = $('.video-autoplay > iframe');
if (iframeVideo.length !== 0) {
try {
let _this = $('.video-autoplay > iframe');
let video = _this.attr('src');
let videoId = _this.attr('src').split('/').pop();
_this.attr(
'src',
`${video}?autoplay=1&mute=1&vol=0&loop=1&playlist=${videoId}`
);
} catch (error) {
console.log(error.message);
}
}

Related

Youtube Video crash app iOS (Received memory warning)

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];

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