【问题标题】:UIWebView youtube video player DONE button dismisses View Controller to main app rootViewControllerUIWebView youtube 视频播放器完成按钮将视图控制器关闭到主应用程序 rootViewController
【发布时间】: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


    【解决方案1】:

    整个问题是有人viewDidAppear 或实际上是NavigationViewController 接到电话,因为它会带你回到应用程序的主要或实际上rootViewController。它正在清理所有以前的ViewControllers 并带我回到那里。

    我用断点调试并发现了问题。所以,我的解决方案是在视频即将结束之前在userDefaults 中设置一个变量,并调用该函数进行设置这个变量,然后在我的viewDidAppear 中使用 if 条件跳过代码,ViewController 的所有重新设置在我的情况下发生了什么......

    好吧,我希望它可以帮助遇到相同或类似问题的其他人..

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-20
      相关资源
      最近更新 更多