I need to know when a youtube video has stopped playing. I looked up existing Q&As and all of them suggested redirecting to another unique URL which can be detected in webview load finished delegate.
But to do this I need to have a HTML page on server or in my app(since I need to add javascript functions for onStateChange
). If its on server, server downtime will affect the app, if its in the app, any change would require an app update.
Is there any other workaround for this ?
EDIT: JS code which needs to be injected/loaded in html.
var player;
var videoID;
var prmstr = window.location.search.substr(1);
var prmarr = prmstr.split ("&");
var params = {};
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
if (params.videoID !== undefined || params.videoID !== "") {
videoID = params.videoID;
} else {
videoID = "0Bmhjf0rKe8";
}
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: videoID,
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
window.location.href = "http://example.com/done.html";
}
}