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;
}
youtube-ios-player-helper不工作,即使他們提供的演示也無法正常工作 – 2015-08-11 05:44:06