【发布时间】:2023-03-07 15:56:01
【问题描述】:
我的应用中的 UIImageView (headerView) 上有一个播放按钮。用户单击此按钮后,我想直接在此 UIImageView 的框架内显示嵌入的 YoutubeVideo。但是,首先我的 Youtube 视频没有自动播放,第二个尺寸太小了。我究竟做错了什么?我一直在谷歌搜索所有可能的答案并尝试了一切。还是与模拟器相关的问题?这是我通过点击播放按钮触发的代码:
NSString *videoIdentifier = @"EXaEz0mJXWY";
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.headerView.bounds];
[webView.configuration setAllowsInlineMediaPlayback:YES];
[webView.configuration setMediaTypesRequiringUserActionForPlayback:WKAudiovisualMediaTypeNone];
[self.headerView 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}})\
}\
function onPlayerReady(a)\
{ \
a.target.playVideo(); \
}\
</script>\
<iframe id='playerId' type='text/html' width='%0.0f' height='%0.0f' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
</body>\
</html>", webView.frame.size.width, webView.frame.size.height, videoIdentifier];
//NSLog(@"Embeddedhtml: %@", embedHTML);
[webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];
【问题讨论】:
标签: objective-c wkwebview youtube-iframe-api