今天在调用播放视频控件的时候,当 MPMoviePlayerViewController 成功读取 url Link 加载执行 presentViewController 操作时遇到了异常;
异常导致视频 push 进 view 后屏幕处于黑屏状态,只发音却未渲染出图像内容;
通过控制台的 Error 提示捕获到 whose view is not in the window hierarchy! 该异常提示;
通过 debug 后发现是当前要渲染的页面未被初始化就调用了 VC 内的方法,导致视图未能成功加载出来.
解决办法:
在执行完 code 逻辑后,即将调用 presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#> 方法前添加如下代码即可,代码如下:
一.代码创建视图方法
while (vc.presentedViewController) {// 解决 whose view is not in the window hierarchy!
vc = vc.presentedViewController;
}
二.Storyboard 创建视图方法
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController* loginViewController = [mainstoryboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[self.window makeKeyAndVisible];
//[LoginViewController presentViewController:yourController animated:YES completion:nil];
//or
[LoginViewController myMethod];
其中,上述 code 中的 vc 为 UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController; 即你将要 presentViewController 的 VC.
示例 Code 如下:
UIViewController *vc = kKeyWindow.rootViewController;
NSString *video = @"";
NSString *src = [NSString stringWithFormat:@"%@", dicCommand[@"args"][@"src"]];
if (!kStringIsEmpty(src)) {
if ([src rangeOfString:@".mp4"].location != NSNotFound) {// 当前字符串中是否包含 .mp4
video = src;
}
else {
video = [NSString stringWithFormat:@"%@.mp4", src];
}
NSLog(@"Current Video Link --- \n%@", video);
MPMoviePlayerViewController *mpc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:video]];
while (vc.presentedViewController) {// 解决 whose view is not in the window hierarchy!
vc = vc.presentedViewController;
}
[vc presentViewController:mpc animated:YES completion:nil];
}
else {
[MBProgressHUD showError:@"暂无视频资源" toView:vc.view];
}
网友评论