2014-12-03 65 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