Is it possible to play YouTube videos on iOS without revealing / adding a presentation, but still following the YouTube Terms of Service?

I have a UITableView full of thumbnails of videos on YouTube, and when they click on it, I want to automatically start full-screen viewing of videos on YouTube without the user seeing the added view or having to interact further than clicking on the thumbnail.

Basically, I don't want to see a YouTube video player with a red play icon at all.

I planned to use youtube-ios-player-helper / YTPlayerView for this, and I understand that it just uses a UIWebView , but I can't seem to figure out how to make it work.

If I create an instance variable in my class, set myself as a delegate and select a random video to view:

 let YouTubePlayer = YTPlayerView() override func viewDidLoad() { super.viewDidLoad() YouTubePlayer.delegate = self YouTubePlayer.loadWithVideoId("apKJikXWU2g") ... } 

and then when calling the delegate method:

 func playerViewDidBecomeReady(playerView: YTPlayerView!) { YouTubePlayer.playVideo() } 

But most of the time it either crashes in my AppDelegate with this message:

Nov 5, 23:34:44 rtcreporting [73827]: registration begins ...

November 5, 23:34:44 rtcreporting [73827]: setMessageLoggingBlock: called

Or it will work if I turn off the control points, but before I receive the video, I get a ton of messages about the restriction of the layout machine, showing that something is angry at some level.

Is it because I'm using a subclass of UIView without adding it to the view hierarchy?

How can I perform a YouTube auto-play behavior after a specific event without revealing an intermediate view?

+6
source share
3 answers

Here you go, you do not need to see the youtube logo.

  • Define youtube iFrame, you will need to replace the '%@' your youtube video id later when you will supply the UIWebView

     #define youtubeVideoPlayUrl @"<html><head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:'%f', height:'%f', videoId:'%@', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>" 
  • Define a UIWebView object named videoWebView to display an iFrame. You can set the height and width to zero so that it does not block your screen. It looks like a hidden iFrame.

     self.videoWebview = [UIWebView alloc] init]; 
  • I suggest you use your own default image and a play button on top of the image so that you don’t have to show the Youtube video logo. After you have done this, use the following code to play the full-screen video on YouTube.

     - (void)onPlayYouTubeVideo:(UIButton *)sender { NSString *youtubeVideoID = YOUR_YOUTUBE_VIDEO_ID; NSString *html = [NSString stringWithFormat:youtubeVideoPlayUrl, _videoWebView.frame.size.width, _videoWebView.frame.size.height, youtubeVideoID]; _videoWebView.mediaPlaybackRequiresUserAction = NO; _videoWebView.delegate = self; [_videoWebView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]]; } 

I use this as a hotel booking application.

+6
source

I have created a demo application for you.

Source code for downloading webView html, taken from: http://iphoneincubator.com/blog/audio-video/how-to-play-youtube-videos-within-an-application

ViewController Header File

 @interface ViewController : UIViewController @property (nonatomic, strong) UIWebView *webView; @end 

ViewController Implementation File

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)]; self.webView.layer.borderWidth = 1.0; self.webView.layer.borderColor = [UIColor redColor].CGColor; self.webView.backgroundColor = [UIColor blackColor]; self.webView.center = self.view.center; [self.view addSubview:self.webView]; 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>"; NSURL *url = [NSURL URLWithString:@"https://www.youtube.com/watch?v=K14RnTVt194"]; NSString* html = [NSString stringWithFormat:embedHTML, url, self.webView.frame.size.width, self.webView.frame.size.height]; [self.webView loadHTMLString:html baseURL:nil]; } 

You should see this:

screenshot of demo app

When you press the play button, it goes into full screen mode.

You probably want to create a method that accepts the URL for a YouTube video in your custom cell class.

Then, in the cellForRowAtIndexPath: method of your view controller, you can call your custom cell loadYouTubeVideoWithURL: Youtube video url load method from your data source.

+5
source

The YouTube Helper Library is a great way to embed Youtube in your iOS app while fulfilling the YouTube Terms of Service.

Regarding layout constraint log warnings,

Is it because I'm using a subclass of UIView without adding it to the view hierarchy?

These errors have nothing to do with your implementation and are easy to reproduce.

You can create an empty iOS 8 project, add a UIWebView to your interface, and try loading the Youtube page.

 [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUTUBE_URL"]]]; 

You will receive the same restriction log messages that you received when using the YouTube Helper Library .

If you take a closer look at items that have problems with auto-layout restrictions, you'll see AVAudioOnlyIndicatorView, AVUnsupportedContentIndicatorView, AVExternalPlaybackIndicatorView, etc. The warning restrictions are not related to the views you created, but are related to the structure of Apple's internal views. This has nothing to do with the YouTube Helper Library or your implementation.

Be sure to include the report: https://bugreport.apple.com

These warnings will not force your application to reject or reduce your chance of approving your application until the problem is fixed by Apple, especially if you can receive warnings using an empty project with UIWebView loading any Youtube page.

+3
source

Source: https://habr.com/ru/post/977804/


All Articles