ajtc
ajtc

Reputation: 23

Youtube API on IOS doesn't send events

I am attempting to allows users of an app to watch streamed videos. I am using the current youtube API in an iFrame and can get the video to display, however none of the events (specifically, I am interested in onStateChange) to call.

onYouTubePlayerAPIReady IS successfully called.

Here is the html script I am loading into the UIWebView (I am listening for the 'window.location = ...' which is how I can tell onYouTubePlayerAPIReady() is being called; that is purely debug):

NSString* htmlString = @"<!DOCTYPE html> \


<html> \
<head> \
<style>body{margin:0px 0px 0px 0px;}</style> \
</head> \
<body> \
<div id=\"player\"></div> \
<script> \
var tag = document.createElement('script'); \
tag.src = \"http://www.youtube.com/player_api\"; \
var firstScriptTag = document.getElementsByTagName('script')[0]; \
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); \
var player; \
 \
function onYouTubePlayerAPIReady() { \
window.location = 'myapp:myaction:param1:param2';\
    player = new YT.Player('player', { width:480, height:360, videoId:'-0Xa4bHcJu8', events: { 'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange } }); \
} \
\
function onPlayerReady(event) { \
window.location = 'myapp:myaction:param1:param2';\
    event.target.playVideo(); \
} \
\
function onPlayerStateChange(event) { \
window.location = 'myapp:myaction:param1:param2';\
    if (event.data == YT.PlayerState.ENDED) { \
        window.location = 'myapp:myaction:param1:param2';\
    } \
} \
</script> \
</body> \
</html>";

[webView_ loadHTMLString:htmlString baseURL:nil];

Upvotes: 1

Views: 249

Answers (1)

Nagaraj
Nagaraj

Reputation: 802

I am also trying to auto play the video, in the same manner. It worked for me when i did not pass nil to base URL, and now it stopped working, Just try changing base url from nil to [[NSBundle mainBundle] resourceURL].

Upvotes: 1

Related Questions