gpt4 book ai didi

ios - YouTube 播放器的事件在 UIWebView 中不起作用

转载 作者:行者123 更新时间:2023-12-03 05:42:47 24 4
gpt4 key购买 nike

我想自动播放电影并在 UIWebView 上自动播放下一部电影。我在 UIWebView 上嵌入了 YouTube 播放器并使用了一些 YouTube Player API for iFrame片段,但它们效果不佳。

HTML 如下。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
body {
background-color: #000;
margin: 0;
}
</style>
</head>
<body>
<div id="player"></div>

<script>
var tag = document.createElement("script");
tag.src = "http://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player("player", {
height: "390",
width: "640",
videoId: "UqFvrjhbO8c",
events: {
"onReady": onPlayerReady,
"onStateChange": onPlayerStateChange
}
});
}

function onPlayerReady(event) {
event.target.playVideo();
}

function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
document.location = "api://didEndedMovie";
}
}
</script>
</body>
</html>

电影结束时,我希望加载 webview api://didEndedMovie .然后,WebView 委托(delegate)将接收到加载的事件,并调用下面的委托(delegate)方法。
- (void)viewDidLoad
{
[super viewDidLoad];

if (!_htmlTemplate) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"YouTubePlayer" ofType:@"html"];
_htmlTemplate = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
}
NSString *htmlString = [NSString stringWithFormat:_htmlTemplate];
[self.webView loadHTMLString:htmlString baseURL:nil];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestString = [[request URL] absoluteString];

// if delegate received requests like "api://*", this view controller
// will not move to other views.

if ([requestString rangeOfString:@"api://"].location == NSNotFound) {
return YES;
}

// called when the movie ended
if ([requestString isEqualToString:@"api://didEndedMovie"]) {
NSLog(@"didEndedMovie");
}

return NO;
}

但是,javascript 不起作用...事件 onReadyonStateChange不被解雇。

在浏览器上,这个 javascript 运行良好。

为什么不触发事件?

最佳答案

这是一个较老的问题,我不知道您是否还需要帮助,所以我会简明扼要。

不要使用 [webView loadHTMLString:baseURL],而是使用 [webView loadRequest]。 loadRequest 方法能够完全加载引用的 YouTube iframe javascript,并且您能够与 iframe api 交互。然后您将看到 onReady 和 onStateChange 事件触发。您还可以调用提示函数和其他东西。如果这不能回答问题,请告诉我。

关于ios - YouTube 播放器的事件在 UIWebView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13548370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com