I am using this code to see youtube videos
- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame;
{
if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];
// HTML to embed YouTube video
NSString *youTubeVideoHTML = #"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%#\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];
// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}
return self;
}
Now I am not able to see the relative video on the simulator..however white webview is coming there..
so is there anything like that that I cannot see youtube videos on simulator or there is any other problem in the code?
You can't see Youtube video in simulator. If white webview is showing, I think your code works.
Try installing on your iPhone/iPod touch.
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 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).
I am trying to embed a video with the following code to avoid launching the youtube app and stay in my app:
-(void)embedYouTube:(NSString*)url frame:(CGRect)frame {
NSString* embedHTML = #"<iframe type=\"text/html\" width=\"64\" height=\"39\" src=\"http://www.youtube.com/embed/j8Bc7eRTdWY\" frameborder=\"0\"></iframe>";
if(videoView == nil) {
videoView = [[UIWebView alloc] initWithFrame:frame];
[movieDescView addSubview:videoView];
}
[videoView loadHTMLString:embedHTML baseURL:nil];
}
The problem is when I clicked on the play icon, it opens the youtube website with my uiwebview asking to install some web app . I would like to prevent this web app pop up to show or start the video full screen. Actually, any solution to show a youtube video without exiting my app would do.
You can use this to load the movie (no needed to generate a html string):
movieView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
movieView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com/embed/-h4zTEwgCpQ"]];
[movieView loadRequest:request];
[self.view addSubview:movieView];
#pragma mark -
#pragma mark UIWebViewDelegate
//To check if youtube try to show the overview page of m.youtube.com or the current movie
//if the user click on the youtube logo this method stop loading the mobile page
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *urlRequest = [request URL];
if ([[urlRequest absoluteString] isEqualToString:url]) {
return YES;
}
return NO;
}
The onely thing you have to looking for, is to use the "embed" link.
UIWebView *wbView = (UIWebView *)[self.view 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 your video url
You don't use embed if you are trying to play the video. Just use the normal video URL so it will open in the iPhone's YouTube application rather than the web. If it opens in the web it will ask to install it's web app.
You need to change your HTML to this:
Play Video
Or to launch it directly to the native app use the URL:
http://www.youtube.com/watch?v=j8Bc7eRTdWY
More Reference Information:
Apple Reference on URL Schemes
Apple Reference on Youtube Links
Tips:
stringUrl = [stringUrl stringByReplacingOccurrencesOfString:#"/watch?v=" withString:#"/embed/"];
where stringUrl is a video link of youtube ( ex. Youtube )
I spent quite a bit of time looking through libraries and old code snippets; this library worked for me:
https://github.com/larcus94/LBYouTubeView
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];
}