How to play YouTube video in iOS not in fullscreen

2019-08-06 13:38发布

I've been trying to get YouTube music videos to play in my iOS app in a small window (not full screen) and can't get it to work. I've tried the following:

  1. I first used the YouTube API and created a YTPlayerView as instructed here: https://developers.google.com/youtube/v3/guides/ios_youtube_helper#adding_ytplayerview. This allowed me to play videos in-line, but many videos were unable to be played because of licensing issues, stating "This video contains content from *. It is restricted from playback on certain sites."
  2. I then tried using a UIWebView, which successfully played the music videos that were previously unavailable. However, these videos were always forced to play in fullscreen mode, even when I set webView.allowsInlineMediaPlayback = YES;
  3. I also tried embedding the video using the technique discussed here: How to embed YouTube video on iOs and play it directly on UIWebview without full screen. This worked to keep videos inline, but again, rendered certain videos unplayable.
  4. I finally tried using an MPMoviePlayerController, but later found out that it can't actually play YouTube videos, as we don't have access to the direct links of video files, so this was another no-go.

Apps like PingTune were clearly able to figure this out using YouTube - they are able to play YouTube music videos in a custom, non-fullscreen view and without ads - what am I missing? Is there a part of the YouTube API that allows me to play these music videos that I implemented incorrectly?

1条回答
Anthone
2楼-- · 2019-08-06 14:18

I've never tried it myself but you could try LBYouTubeView by Laurin Brandner. LBYouTubeView is just a small view that is able to display YouTube videos in a MPMoviePlayerController. You even have the choice between high-quality and standard quality stream.

Code sample

LBYouTubePlayerViewController* controller = [[LBYouTubePlayerViewController alloc] initWithYouTubeURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=1fTIhC1WSew&list=FLEYfH4kbq85W_CiOTuSjf8w&feature=mh_lolz"] quality:LBYouTubeVideoQualityLarge];
controller.delegate = self;
controller.view.frame = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f);
controller.view.center = self.view.center;
[self.view addSubview:self.controller.view];
查看更多
登录 后发表回答