0

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";
        }
    }
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
  • Can't you inject javascript into the page when it's loaded? Then you can add whatever handlers you need. Check out http://stackoverflow.com/questions/8886443/calling-javascript-using-uiwebview – NG. Dec 23 '13 at 05:16
  • @SB. If I have to inject JS, I need to inject above mentioned full code, as I have reference to only Embedded video id, which looks kinda messy if injected. – GoodSp33d Dec 23 '13 at 05:43

0 Answers0