我正在尝试嵌入 youtube 视频并在我的应用程序中自动播放。该代码无法在 iOS6 上运行,但它可以在较旧的 iOS 5 上完美运行。
我这样做:
-(IBAction)playVideo:(id)sender {
myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
myWebView.delegate = self;
[myWebView setAllowsInlineMediaPlayback:YES];
myWebView.mediaPlaybackRequiresUserAction=NO;
[myWebView loadHTMLString:[NSString stringWithFormat:@"<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" width=\"300\" height=\"300\"></embed>", @"http://www.youtube.com/watch?v=TbsXUJITa40"] baseURL:nil];
}
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
UIButton *b = [self findButtonInView:webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
所以 - 当 webview 被加载时,它会自动找到 uibutton 并且视频开始。我不明白,为什么在 iOS 6 中这种方法不再起作用。它加载了视频,但什么也没有出现...
任何人都可以帮助我吗?我要疯了,试图解决它...