1

Is there not an option in the youtube javascript api to launch a video (by id/url) on mobile phones? Right now I have a list of videos (data downloaded via php from youtube api) and I'm displaying the thumbnail of each video with the title/date/etc beside it.

Ideally I'd like the video to play when a user taps the thumbnail but I can't figure out how to do this without having the JS api create an iframe of the video (in an overlay) but then the user has to tap that overlay again to play the video since the api doesn't allow autoplay on phones.

Does anyone know of a solution to this?

Joe
  • 1,762
  • 9
  • 43
  • 60

2 Answers2

1

If you are using Android you should utilize Android Player. For anything else, you should utilize IFrame Player. AutoPlay won't work in iOS due to Apple's own terms though.

Even if you find a workaround, you'd be violating those terms and I would not suggest that.

Ibrahim Ulukaya
  • 12,767
  • 1
  • 33
  • 36
0

the fiddle. Here is the code:

<div id="timerText"></div>
<iframe id="player" width="420" height="315" src="http://www.youtube.com/embed/TP2iRzzamCk" frameborder="0" allowfullscreen></iframe>

<body >

<script>
/**
 * @author       Rob W <gwnRob@gmail.com>
 * @website      http://stackoverflow.com/a/7513356/938089
 * @version      20120724
 * @description  Executes function on a framed YouTube video (see website link)
 *               For a full list of possible functions, see:
 *               https://developers.google.com/youtube/js_api_reference
 * @param String frame_id The id of (the div containing) the frame
 * @param String func     Desired function to call, eg. "playVideo"
 *        (Function)      Function to call when the player is ready.
 * @param Array  args     (optional) List of arguments to pass to function func*/
function callPlayer(frame_id, func, args) {
    if (window.jQuery && frame_id instanceof jQuery) frame_id = frame_id.get(0).id;
    var iframe = document.getElementById(frame_id);
    if (iframe && iframe.tagName.toUpperCase() != 'IFRAME') {
        iframe = iframe.getElementsByTagName('iframe')[0];
    }

    // When the player is not ready yet, add the event to a queue
    // Each frame_id is associated with an own queue.
    // Each queue has three possible states:
    //  undefined = uninitialised / array = queue / 0 = ready
    if (!callPlayer.queue) callPlayer.queue = {};
    var queue = callPlayer.queue[frame_id],
        domReady = document.readyState == 'complete';

    if (domReady && !iframe) {
        // DOM is ready and iframe does not exist. Log a message
        window.console && console.log('callPlayer: Frame not found; id=' + frame_id);
        if (queue) clearInterval(queue.poller);
    } else if (func === 'listening') {
        // Sending the "listener" message to the frame, to request status updates
        if (iframe && iframe.contentWindow) {
            func = '{"event":"listening","id":' + JSON.stringify(''+frame_id) + '}';
            iframe.contentWindow.postMessage(func, '*');
        }
    } else if (!domReady || iframe && (!iframe.contentWindow || queue && !queue.ready)) {
        if (!queue) queue = callPlayer.queue[frame_id] = [];
        queue.push([func, args]);
        if (!('poller' in queue)) {
            // keep polling until the document and frame is ready
            queue.poller = setInterval(function() {
                callPlayer(frame_id, 'listening');
            }, 250);
            // Add a global "message" event listener, to catch status updates:
            messageEvent(1, function runOnceReady(e) {
                var tmp = JSON.parse(e.data);
                if (tmp && tmp.id == frame_id && tmp.event == 'onReady') {
                    // YT Player says that they're ready, so mark the player as ready
                    clearInterval(queue.poller);
                    queue.ready = true;
                    messageEvent(0, runOnceReady);
                    // .. and release the queue:
                    while (tmp = queue.shift()) {
                        callPlayer(frame_id, tmp[0], tmp[1]);
                    }
                }
            }, false);
        }
    } else if (iframe && iframe.contentWindow) {
        // When a function is supplied, just call it (like "onYouTubePlayerReady")
        if (func.call) return func();
        // Frame exists, send message
        iframe.contentWindow.postMessage(JSON.stringify({
            "event": "command",
            "func": func,
            "args": args || [],
            "id": frame_id
        }), "*");
    }
    /* IE8 does not support addEventListener... */
    function messageEvent(add, listener) {
        var w3 = add ? window.addEventListener : window.removeEventListener;
        w3 ?
            w3('message', listener, !1)
        :
            (add ? window.attachEvent : window.detachEvent)('onmessage', listener);
    }
}
    var secs = 10;
    var currentSeconds = 0;
    var currentMinutes = 0;
    setTimeout('Decrement()',1000);

    function Decrement() {
        currentMinutes = Math.floor(secs / 60);
        currentSeconds = secs % 60;
        if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds;
        secs--;
        document.getElementById("timerText").innerHTML = currentMinutes + ":" + currentSeconds; //Set the element id you need the time put into.
        if(secs !== -1) {
            setTimeout('Decrement()',1000);
        }else{
            callPlayer('player', 'playVideo');
        }
    }
</script>
  • That works on tablet/desktop, not on mobile phones (iphone for example). Did not check on Android/Windows Phone though. – Joe Jun 10 '13 at 17:34
  • @Joe Well the api works good for platforms with the flash player installed (so not for every smartphone/tablet), anyway there is the possibility to use that api with iOS but I guess not in this case. Just to report https://groups.google.com/forum/?fromgroups#!topic/youtube-api-gdata/YQecs6a5lzE – Alessandro Gabrielli Jun 10 '13 at 17:58