I want to play movies automatically and go next movie automatically on UIWebView. I embeded YouTube Player on UIWebView and use some YouTube Player API for iFrame snippets, but they didn't work well.
HTML is below.
<!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>
When the movie ended, I want the webview to load api://didEndedMovie
. Then, WebView delegate will receive loaded event, and will call bellow delegate method.
- (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;
}
But, javascript doesn't work... The events onReady
and onStateChange
are not fired.
On browser, this javascript works well.
Why are not the events fired?