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