2014-12-03 67 views
3

我在我的应用程序用于播放YouTube的视频这个要求:的Youtube API在IOS 7和IOS 8

  • 自动播放时,容器视图控制器会出现
  • 检测时,视频结束(我在这个事件使能控制)
  • 它开始不是全屏
  • 支持全屏portrail和景观
  • 充满IOS 7兼容和8

首先,我尝试的YouTube-IOS玩家辅助这个结果:

  • IOS 8:工作了完美
  • IOS 7:全屏,不会播放液每次视频暂停。

我的东西,这可能是更好的解决方案,但我不能让它正常工作在IOS 7

第二和实际的选择,我用的混合解决方案: IOS 8与YouTube-IOS-播放器 - 助手和带有UIWebView的IOS 7并嵌入youtube播放器。结果:

  • IOS 8:工作了完美
  • IOS 7:工作,但1/5的视频自动播放不能正常工作,因此没有检出视频结束(我不知道为什么)

我明白任何建议有关更好的解决方案,

我的实际代码(第二个选项):

-(void)youtubePlayerConfiguration{ 

NSString *version = [[UIDevice currentDevice] systemVersion]; 


if ([version floatValue] >= 8.0) { 

    NSDictionary *playerVars = @{ 

           @"playsinline" : @1, 
           @"autoplay" : @1, 
           @"showinfo" : @0, 
           @"autohide" : @1, 
           @"rel" : @0, 
           @"modestbranding" : @1 
           }; 

    youtubePlayer.delegate = self; 
    [youtubePlayer loadWithVideoId:self.videoId playerVars:playerVars]; 

} else { 
    UIWebView *webView = [[UIWebView alloc] initWithFrame:youtubePlayer.frame]; 
    [webView setAllowsInlineMediaPlayback:YES]; 
    [webView setMediaPlaybackRequiresUserAction:NO]; 
    webView.delegate = self; 

    [self.view addSubview:webView]; 

    NSString* embedHTML = [NSString stringWithFormat:@"\ 
          <html>\ 
          <body style='margin:0px;padding:0px;'>\ 
          <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\ 
          <script type='text/javascript'>\ 
          function onYouTubeIframeAPIReady()\ 
          {\ 
          ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady,onStateChange: onPlayerStateChange}})\ 
          }\ 
          function onPlayerReady(a)\ 
          { \ 
          a.target.playVideo(); \ 
          }\ 
          var done = false;\ 
          function onPlayerStateChange(event) {\ 
           if (event.data == YT.PlayerState.PLAYING && !done) {\ 
            setTimeout(stopVideo, 6000);\ 
            done = true;\ 
           }\ 
           if (event.data == YT.PlayerState.ENDED) {\ 
            window.location = 'callback:anything';\ 
           }\ 
          }\ 
          </script>\ 
          <iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1&showinfo=0' frameborder='0'>\ 
          </body>\ 
          </html>", 288,150, self.videoId]; 
    [webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]]; 

    webView.allowsInlineMediaPlayback = YES; 
    webView.mediaPlaybackRequiresUserAction = NO; 
    webView.scrollView.bounces = NO; 

    youtubePlayer.hidden = YES; 
} 

} 


#pragma mark - Player YouTube Delegates 

-(void)playerViewDidBecomeReady:(YTPlayerView *)playerView{ 
    //[[NSNotificationCenter defaultCenter] postNotificationName:@"Playback started" object:self]; 

    [playerView playVideo]; 
} 

- (void)playerView:(YTPlayerView *)playerView didChangeToState:(YTPlayerState)state{ 
    if (state == kYTPlayerStateEnded) { 
     ratingView.editable = YES; 
     ratingView.alpha = 1; 
     ratingView.userInteractionEnabled = YES; 
    } 
} 

- (void)playerView:(YTPlayerView *)playerView receivedError:(YTPlayerError)error 
{ 
    NSLog(@"YTPlayerView : receivedError :%i",error); 
} 

#pragma mark - WebView Delegate 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { 
    NSLog(@"ENTRO - request:%@",request); 

    if ([[[request URL] scheme] isEqualToString:@"callback"]) { 

     NSLog(@"get callback"); 
     ratingView.editable = YES; 
     ratingView.alpha = 1; 
     ratingView.userInteractionEnabled = YES; 
     return NO; 

    } 

    return YES; 
} 

回答

2

我WA s使用下面的代码为iOS 7和它工作正常,我现在使用iOS 8,所以我使用youtube-ios-player-helper

-(void)initializeYouTubePlayerWithVideoID(NSString *)videoId 
{ 
    NSString *youTubeVideoHTML = @"<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:'1024', height:'728', videoId:'%@', playerVars: {controls:0,rel:0, modestbranding:1, html5:0, showinfo:0}, events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); } function onPlayerReady(event) { event.target.playVideo(); } function onPlayerStateChange(event) { if (event.data == YT.PlayerState.ENDED){ } } </script> </body> </html>"; 

    NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId]; 

    _youtubePlayerView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]; 
    _youtubePlayerView.backgroundColor = [UIColor blackColor]; 
    _youtubePlayerView.delegate = self; 
    _youtubePlayerView.allowsInlineMediaPlayback = NO; 

    [self.view addSubview:_youtubePlayerView]; 

    _youtubePlayerView.mediaPlaybackRequiresUserAction = NO; 
    [_youtubePlayerView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerDidExitFullscreen:) 
              name:@"UIMoviePlayerControllerDidExitFullscreenNotification" 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playerDidEnterFullscreen:) 
               name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" 
              object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playbackStateDidChange:) 
              name:@"MPAVControllerPlaybackStateChangedNotification" 
              object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(playbackDidEnd:) 
              name:@"MPAVControllerItemPlaybackDidEndNotification" 
              object:nil]; 
} 

-(void)playbackStateDidChange:(NSNotification *)notification 
{ 
    switch ([[notification.userInfo objectForKey:@"MPAVControllerNewStateParameter"]  intValue]) 
    { 
     case 0: 
      //Loading 
      break; 
     case 1: 
      //Paused 

      break; 
     case 2: 
      //Playing"; 
      break; 
     case 3: 
      //Buffering"; 
     break; 

     default: 
      break; 
    } 
} 

-(void)playbackDidEnd:(NSNotification *)notification 
{ 
    //Playback Ended 
} 

-(void)playerDidExitFullscreen:(NSNotification *)notification 
{ 
    //playerDidExitFullscreen 
} 

-(void)playerDidEnterFullscreen:(NSNotification *)notification 
{ 
    //playerDidEnterFullscreen 
} 
+0

youtube-ios-player-helper不工作,即使他们提供的演示也无法正常工作 – 2015-08-11 05:44:06