【发布时间】:2014-10-05 18:12:23
【问题描述】:
问题..
每当我在 youtube (嵌入在 iframe 中)视频是从 UIWebView 启动的,这不是我想要的。我只想回到这个 youtube 视频启动的视图控制器,并显示该视图中的所有内容。
我尝试使用通知来捕捉 UIMoviePlayerController 的状态,以呈现之前启动 youtube 视频的 View Controller。但是,我也在使用导航控制器,而且这个视图控制器依赖于之前的控制器选择,它使用一些数据来显示视图控制器中的内容。
我真的需要帮助,我不知道是什么问题..!!
建议? 我需要设置自己的 MPMoviePlayerController 吗?如果是这样,我是否必须实现一些委托才能控制其状态以及它在做什么?
这就是我将 UIWebView 设置为 TableView 中的某一行的方式
self.videoView = [[UIWebView alloc] initWithFrame:CGRectMake(kElementX, kElementY, kElementWidth, 120)];
self.videoView.backgroundColor = [UIColor clearColor];
self.videoView.opaque = NO;
self.videoView.scalesPageToFit = YES;
//self.videoView.delegate = self;
[self.videoView setTranslatesAutoresizingMaskIntoConstraints:YES];
self.videoView.allowsInlineMediaPlayback = YES;
[cell.contentView addSubview:self.videoView];
NSString *youtubeURL = [NSString stringWithFormat:@"%@", self.url];
NSLog(@"youtube link -> %@", youtubeURL);
NSString *videoHTML = [NSString stringWithFormat:@"\
<html>\
<head>\
<style type=\"text/css\">\
iframe {position:absolute; top:0%%; margin-top:0px;}\
body {background-color:#000; margin:0;}\
</style>\
</head>\
<body>\
<div id=\"player\">\
<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"420px\" src=\"%@\" frameborder=\"0\" allowfullscreen></iframe>\
</div>\
<script>\
var tag = document.createElement('script');\
tag.src = \"https://www.youtube.com/iframe_api\";\
var firstScriptTag = document.getElementsByTagName('script')[0];\
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);\
var player;\
var myVideoId = document.getElementById('%@')\
function onYouTubeIframeAPIReady() {\
player = new YT.Player('player', {\
height: '100%%',\
width: '100%%',\
videoId: myVideoId,\
playerVars:{\
'autoplay':0,\
'controls':1,\
'enablejsapi':1,\
'playsinline':1,\
'showinfo':0\
events: {\
'onReady': onPlayerReady,\
}\
});\
}\
</script>\
</body>\
</html>", youtubeURL, self.url];
[self.videoView loadHTMLString:videoHTML baseURL:nil];
viewDidLoad 方法...
-(void)viewDidLoad{
// Using notifications to get the state of the UIMoviePlayerController
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieIsPlaying:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieStopedPlaying:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];
}
// Trying to stop the dismiss of the View Controller
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.videoLaunched) {
[super viewWillDisappear:NO];
[self dismissViewControllerAnimated:NO completion:nil];
}
}
// apparently this function besides the notifications functions is the only one which gets call after the done button is pressed (here I tried to attempt loading back the View Controller) not much luck
-(void)viewWillDisappear:(BOOL)animated{
if(self.videoLaunched)
[super viewWillDisappear:NO];
}
// Starts playing the video
- (void)movieIsPlaying:(NSNotification *)notification
{
self.videoLaunched = YES;
}
// The video stop and exit player
- (void)movieStopedPlaying:(NSNotification *)notification
{
self.videoLaunched = NO;
}
我真的希望有人遇到过这个问题并能够与我分享解决方案。谢谢! :)
【问题讨论】:
标签: objective-c ios7 uiviewcontroller uiwebview youtube-api