我对在我的应用中播放YouTube的视频有以下要求:
- 容器视图控制器出现时自动播放
- 检测视频何时完成(在此事件中启用控件(
- 它开始不是全屏
- 支持全屏端口和横向
- 与 IOS 7 和 8 完全兼容
首先,我尝试youtube-ios-player-helper,结果如下:
- IOS 8:完美工作
- IOS 7:视频每次全屏暂停,播放不流畅。
我认为这可能是更好的解决方案,但我无法使其在 IOS 7 中正常工作。
第二个也是实际选项,我使用混合解决方案:带有youtube-ios-player-helper的IOS 8和带有UIWebView和嵌入youtube播放器的IOS 7。结果:
- 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;
}
我在 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
}