iOS Force user to watch a YouTube video - ios

Im using this code to play a commercial video in my iOS application:
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:[NSString stringWithFormat:#"<iframe width=\"%f\" height=\"%f\" src=%# frameborder=\"1\" allowfullscreen></iframe>",frame.size.width-10,frame.size.height-10,urlString] baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
I want to force the user to see the video and close the UIWebView once the video is completed without allowing the user to shut the video off.
I think if i can make the Video plays automatically and set the:
videoView.userInteractionEnabled = NO;
that will force the user to watch it.
My Questions:
1- How to force the UIWebView using <iframe> ?
2- How to detect if the video finished playing?
3- Is there any other way (Better One) to play YouTube videos in iOS?
Glad to hear any other ideas or answers, Thanks in advance.

try this:
videoView.userInteractionEnabled = NO;
and you can create a UIButton to close the video.

you can use LBYoutube for youtube videos. see following link
LBYouTube

Related

Audio is not playing in uiwebview in iPhone

i want to play live streaming video in uiwebview it is playing by believe code
UIWebView *web=[[UIWebView alloc]initWithFrame:self.view.bounds];
[web loadHTMLString:htmlString baseURL:nil];
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
web.scalesPageToFit=YES;
web.mediaPlaybackRequiresUserAction=NO;
[self.view addSubview: web];
but the problem is that audio is not playing in simulator or not in any device with that live video what i am doing wrong or what i have missed here
go to Info.plist property list - add a new row - select required background modes, there should be a subrow called Item 0, select app plays audio or streams audio/video using AirPlay. Also check Audio air play in background modes in compatibility.

Play video in app instead of default player

I use the following code to play videos:
MPMoviePlayerViewController *theMovie=
[[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
theMovie.moviePlayer.repeatMode=MPMovieRepeatModeOne;
[self presentMoviePlayerViewControllerAnimated:theMovie];
This causes a the default movie player to cover the app. Instead I want the video to play inside the app itself as follows:
Can this be achieved?
You can set its frame like this:
theMovie.view.frame = CGRectMake(10, 10, 300, 300);
Then without presenting it add it as subview:
[self.view addSubview: theMovie.view];
Hope this helps.. :)
Yes, this can be achieved by using the AVPlayerLayer. Please see examples here:
"AV Foundation Programming Guide", "Putting It All Together: Playing a Video File Using AVPlayerLayer" section
A tutorial from the iosguy.com
Appuccino's "Playing Movies With AVPlayerLayer In iOS 6" video tutporial

How to get a video to play in UIWebView (video is in a ALAsset group)?

Okay maybe the headline is somehow disturbing, I hope to state this so you can understand my problem.
So I have a scrollview which has paging enabled. I want to build something like iOS Photo App.
I load the photos and videos which are on the device via ALAsset and store them in a group.
Now i want to present each object on a separate subview of the scrollview.
For the pictures it is working fine. But for videos I cant make it work.
Here is what i did and what did not work:
First:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString *embedHTML = [NSString stringWithFormat: #"<html><head><style></style></head><body>\
<video id='video_with_controls' width=\"%0.0f\" height=\"%0.0f\" controls>\
<source src='%#' type='video/MOV'/></video><ul>\
</body></html>", self.mainScrollView.frame.size.width, self.mainScrollView.frame.size.height, [assetRepresentation url]];
[webView loadHTMLString:embedHTML baseURL:nil];
then:
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
NSString* embedHTML = [NSString stringWithFormat:#"<video controls> <source src=\"%#\"> </video>", [assetRepresentation url];
[webView loadHTMLString:embedHTML baseURL:nil];
Also I tried to store it in NSData first and then load it from there, but that causes memory problems and wont work.
Any help is highly appreciated!
Edit: As I think UIWebView is the thing that matches the iOS App visually at most, I want to use this and not for example the MPMoviePlayer .. if I am wrong there I would be glad to know how / what else looks like the original App
I guess the video url is local thats why its not working in UiWebView
so the solution would be this:
in delegate method webviewdidstartload,set up MPmovieplayer controller to play inside a UiView and yes it will look like original app as you will be playing it in a view
something like this:
Add MPMoviePlayerController's view to a UIView
When you movie file is located locale you must set the baseURL to your MainBundle and put the full path in the "src"-tag.

Embed Youtube videos :- with contains content from * , it is restricted from playback on certain site

How to embed a Youtube video that has copyrighted content in it.
For example when you try playing this video(http://www.youtube.com/embed/ADBKdSCbmiM) in a UIWebView it says
This Video Contains content from Vevo. It is restricted from playback on certain sites
How would I go about getting videos like this to play in an embedded player or a custom player that I would make using MPMoviePlayer or such. I know this is possible as following app does this. (http://itunes.apple.com/us/app/audioviz-view-your-songs-on/id547001249?mt=8)
Edit
Few videos play in browser but in iOS Simulator its showing
This video contains content from SME . It is restricted from playback on certain site
Thanks for all the help!
You can use player vars while initializing youtube sdk:
NSDictionary *playerVars = #{
#"origin" : #"http://www.youtube.com",
};
[self.playerView loadWithVideoId:#"KOvoD1upTxM" playerVars:playerVars];
Enjoy!
To play the youtube video using uiwebview:
First check if your youtube URL is played or not in browser. If the video doesn't play in the browser it won't play in the device either
You only check in the device; the video doesn't play in simulator
- (void) displayGoogleVideo
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://www.youtube.com/v/oHg5SJYRHA0"; // valid youtube url
NSString *htmlString = [NSString stringWithFormat:#"<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\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
[window addSubview:webView];
[webView release];
}
If you're embedding on a mobile app, you need to set your Referer in your request header for the video. Otherwise, Youtube automatically blacklists you if your Referer field is left blank.
I answered this in detail here. I was experiencing this myself and I fixed it in my case (iOS app using React Native). I can play Vevo, WMG, and all other restricted content now.

iOS movie player controls jump to middle of player at end of video

My application uses HTTP Live Streaming to play video files. We have run into a problem where as soon as the stream ends the player controls jump from the bottom of the player to the middle as seen in this screenshot (http://postimage.org/image/1dvr9u338/). Is this expected behavior and if not, any thoughts on what is happening?
If the application plays a non-live streaming video, such as an mp4, everything works fine. But as soon as we play a live-streaming video (m3u8), such as Apple's sample streaming video (http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8) this issue occurs.
The application supports iOS 4.2+ and the issue occurs on every version. I have tested this with a brand new project that just embeds an MPMoviePlayerController's view and plays the video. Below is some sample code that loads the contentURL, embeds the view, and plays the video.
- (void)viewDidLoad
{
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
moviePlayerController.view.frame = CGRectMake(100, 100, 600, 400);
[self.view addSubview:c.view];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated
{
[moviePlayerController play];
[super viewDidAppear:animated];
}

Resources