I want to embed a youtube video into a webviewer whenever I press a button. I have this code
- (IBAction)testBtn:(id)sender {
NSString *code = #"<iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/1iBIcJFRLBA\" frameborder=\"0\" allowfullscreen></iframe>";
[[self youtubeWebPlayer]loadHTMLString:code baseURL:nil];
}
My problem is, whenever I press the button nothing happens. The webviewer remains blank.
I have put breakpoints in the code to double check that the code is called by my app and it is indeed called.
EDIT***
I have fixed the problem by replacing
[[self youtubeWebPlayer]loadHTMLString:code baseURL:nil];
with
[[self youtubeWebPlayer]loadHTMLString:code baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
Try this one this is working perfect.
You use youtube Url as "http://www.youtube.com/v/YOU_TUBE_VIDEO_ID".
UIWebView * youTubeWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0,0,320,320)];
youTubeWebView.allowsInlineMediaPlayback=YES;
youTubeWebView.mediaPlaybackRequiresUserAction=NO;
youTubeWebView.mediaPlaybackAllowsAirPlay=YES;
youTubeWebView.delegate=self;
youTubeWebView.scrollView.bounces=NO;
NSString *linkObj=#"http://www.youtube.com/v/1iBIcJFRLBA";//#"http://www.youtube.com/v/6MaSTM769Gk";
NSLog(#"linkObj1_________________%#",linkObj);
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \\width=\"320\" height=\"320\"></embed>\\</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, linkObj];
[youTubeWebView loadHTMLString:html baseURL:nil];
[self.view addSubview:youTubeWebView];
you can add this code if you want to embed you tube video in swift
let webViewYouTube = UIWebView()
webViewYouTube.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.width/2)
webViewYouTube.allowsInlineMediaPlayback = true
webViewYouTube.mediaPlaybackRequiresUserAction = false
webViewYouTube.mediaPlaybackAllowsAirPlay = true
webViewYouTube.delegate = self as? UIWebViewDelegate
webViewYouTube.scrollView.bounces = false
webViewYouTube.scrollView.isScrollEnabled = false
let linkObj = "https://www.youtube.com/watch?v=swIoyaBUpEg"
print("linkObj1_________________\(linkObj)")
let embedHTML = " <html><head> <style type=\"text/css\"> body { background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \\width=\"320\" height=\"320\"></embed>\\</body></html>"
let html = String(format: embedHTML, linkObj)
webViewYouTube.loadHTMLString(html, baseURL: nil)
view.addSubview(webViewYouTube)
Related
I want implement play and stop button same sd youtube when webview is loaded with Youtube URL. I used below code but when it loads in webview it starts playing (auto-play). I just want to load it in webview and not auto-play it. How can I do this?
This is my code:
UIWebView *videoview = [[UIWebView alloc] initWithFrame:CGRectMake(10,80,275.0,150)];
NSURL *nsurl=[NSURL URLWithString:strpreview];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[videoview loadRequest:nsrequest];
[self addSubview:videoview];
Thanks in advance
Here is the code to create an load an embedded video in iPhone using iFrame
NSString *yourURL = #"http://www.cloudstringers.com:14556/ingCloud/users/400010003/mp4_320p/efda2f8a618be8e4a36b81d31251752820130710115909.mp4";
NSString *embedHTML =[NSString stringWithFormat:#"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: #666666;\
padding:%f %f %f %f;\
color: blue;\
}\
</style>\
</head><body style=\"margin:0\">\
<iframe height=\"%f\" width=\"%f\" title=\"YouTube Video\" class=\"youtube-player\" src=\"%#" ></iframe>\
</body></html>",paddingTop,paddingRight,paddingBottom,paddingLeft,videoHeight,videoWidth,yourURL];
[self.webView loadHTMLString:embedHTML baseURL:nil];
Hope this helps. Do let me know if you need anything more.
I am using the below code to play youtube url:
NSString *youtubeUrl = [NSString stringWithFormat:#"http://www.youtube.com/v/%#",[photo videoUrl]];
[self embedYouTube:[youtubeUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] frame:CGRectMake(10, 120, 300, 200)];
- (void)embedYouTube:(NSString*)url frame:(CGRect)frame
{
NSString* embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: black;\
color: black;\
}\
</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, frame.size.width, frame.size.height];
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.layer.borderColor = [[UIColor blackColor] CGColor];
[self.view addSubview:videoView];
[videoView setHidden:NO];
}
[videoView loadHTMLString:html baseURL:nil];
}
User need's to tap on the webview to play this video. Can't it directly load the video without tapping on the play icon?
add a parameter autoplay=1 to the URL, like this:
NSString *youtubeUrl = [NSString stringWithFormat:#"http://www.youtube.com/v/%#?autoplay=1",[photo videoUrl]];
Check this way to embed your play, which support Auto PLay
https://developers.google.com/youtube/player_parameters
You should check LBYouTubePlayerController which uses the MoviePlayer component to load youtube videos and it's a lot easier to customize things, like autoplay feature.
I think the short answer is, if you're displaying videos with a webView autoPlay is no longer allowed per Apple guidelines. The docs specifically state:
Warning: To prevent unsolicited downloads over cellular networks at
the user’s expense, embedded media cannot be played automatically in
Safari on iOS—the user always initiates playback.
This same doc is referenced from Google Developer / YouTube API docs.
I have video embedded in my application using this solution (html string in UIWebView):
http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application
Unfortunately client requires his own thumbnail. I would like to solve it by creating UIWebView once he taps on this thumbnail and play the video inside automatically.
How can I do it?
Try this:-
NSString *htmlString = [NSString stringWithFormat:#"<html><head>"
"<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 50\"/></head>"
"<body style=\"background:#F00;margin-top:0px;margin-left:0px\">"
"<div><object width=\"50\" height=\"50\">"
"<param name=\"movie\" value=\"%#\"></param>"
"<param name=\"wmode\" value=\"transparent\"></param>"
"<embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"50\" height=\"50\"></embed>"
"</object></div></body></html>",[d objectForKey:#"hrefUrl"],[d objectForKey:#"hrefUrl"]];
[videoView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.your-url.com"]];
Following is the html string to play video automatically inside UIWebView...
NSString *embedHTML = [NSString stringWithFormat:#"<html><body><video controls=\"controls\" autoplay=\"autoplay\"><source src=\"%#\" type=\"video/mp4\"/></video></body></html>", url];
[_webView loadHTMLString:embedHTML baseURL:baseURL];
I have tried the following one, it is working fine and also starts to play automatically.
NSString *embedHTML = #"<html><body bgcolor="black"><embed id="yt" src="http://www.youtube.com/watch?v=9vyYHVB-QnY"type="application/x-shockwave-flash" width="320.00" height="370.00"></embed></body></html>";
[_webView loadHTMLString:embedHTML baseURL:baseURL];
I am working with mp4 videos hosted by my company, not YouTube.
I am using the following code to create a clickable thumbnail in a UIWebView:
videoButtonHTML = [videoButtonHTML stringByAppendingFormat:#"<body bgcolor=\"#000000\"><img src=\"%#\" width=\"120\" height=\"90\" border=\"0\" type=\"application/x-shockwave-flash\"/></body>", videoURL, self.imageLink];
[videoButtonWebview loadHTMLString:videoButtonHTML baseURL:nil];
This works fine at first. It creates the thumbnail and plays the video. The problem is that, when you close the video, the thumbnail image disappears. I still have the "blank" Quicktime play image. I really want to keep the image, though.
I've tried this both with and without the shockwave tag - same result. I found an embedYoutube function in a tutorial. It works great with YouTube videos, but not with mp4 videos. Here's that code, in case it helps:
- (void)embedYouTube1:(NSString *)urlString frame:(CGRect)frame {
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, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
Any idea what's going on?
Try saving the image as NSData, maybe to your NSUserDefaults (when the video is loaded), then you can display the thumb from defaults, instead of the video itself (when the video is not loaded).
There are some apps in the app store that have a video stream without using mpvideo and they are embbeded on the uiview or uiwebview.
How is this implementation done? Any help is great thanks.
Probably with the standard HTML5 <video> tag. It allows you to play a video file that is local or remote. Or even a live stream. Check Apple's Safari Dev Center for all details.
You can try to load the HTML code into the webview itself with loadHTMLString:
The method is part of UIWebView and looks something like this:
[self.webview loadHTMLString:(NSString *)_some_string baseURL:nil]
You can store a basic html page with the embed code for the video in the _some_string variable and it will load up that page with the embed code.
NSString *html = #"\
<html>\
<head></head>\
<embed code for video here>\
</body></html>";
[self.webView loadHTMLString:html baseURL:nil];
This is a pretty complicated solution due to the way Apple has videos setup.
Create an HTML file with a basic HTML5 video player, with source="video/you/want". Be sure to add webkit-playsinline to the video.
<video width=“320” height=“240” webkit-playsinline><source src=“sample_iPod.m4v”/></video>
Add this player.html as a resource to your Xcode project.
Create a UIWebView, hook it up in the Storyboard editor, and then do the following in ViewDidLoad:
//set the webView delegate - hook up in Storyboard as well!
self.webView.delegate = self;
//initialize our HTML path - the file is player.html in our project
NSString *htmlPath = [[NSBundle mainBundle] pathForResource:#”player” ofType:#”html”];
//encode the path
NSString *escapedPath = [htmlPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//webview needs NSURL, so let’s make one
NSURL *url = [NSURL URLWithString:escapedPath];
//load the request in the webview
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
Finally, the key step, be sure to flag the view to allow inline playback.
//flag the webview to play inline
self.webView.allowsInlineMediaPlayback = YES;
I wrote a post with more details here.
Can you try this one.
- (void)viewDidLoad {
[super viewDidLoad];
NSString *embedHTML = #"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"http://www.businessfactors.de/bfcms/images/stories/videos/defaultscreenvideos.mp4\" type=\"application/x-shockwave-mp4\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 412.0)];
[webView setOpaque:NO];
NSString *html = [NSString stringWithFormat:embedHTML, webView.frame.size.width, webView.frame.size.height];
[webView loadHTMLString:html baseURL:nil];
[self.view addSubview:webView];
}