Later Today: ASP.NET Tutorial 2: Introduction to C#

(jQuery) Make Vimeo Embeds iPhone-friendly… Automagically!

I use the myGlobals object in many of my projects to perform one-off calculations that I may need to re-use all over the place (hence the name… myGlobals). For posterity, this is what my boilerplate myGlobals code looks like…

(JavaScript) myGlobals:

/********************************************************************************
 * myGlobals Object
 *
 * PURPOSE: Used to store some basic (mostly dynamic) global data that may need to be
 * reused throughout an entire javascript application.
 ********************************************************************************/
var myGlobals = {
    /**Gets the uri of the current webpage*/
    uri:        window.location.pathname,
    /**Returns a string formatted with todays date as m-d-yyyy*/
    today:      function(){var now = new Date();return (now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear()},
    /**The value of the anchor for the current page */
    anchor:     unescape(document.location.hash.substring(1)),
    /**True if device is an iPhone*/
    uaiphone:   (navigator.userAgent.toLowerCase().indexOf('iphone')!='-1'),
    /**True if device is an iPad*/
    uaipad:     (navigator.userAgent.toLowerCase().indexOf('ipad')!='-1'),
    /**True if device is Android-based*/
    uadroid:     (navigator.userAgent.toLowerCase().indexOf('android')!='-1')
}

Prerequisites: Familiarity with JavaScript and jQuery. A Vimeo Plus account with at least one video uploaded (unfortunately mobile embedding isn’t yet available to free users).

This article demonstrates the use of a neat bit of jQuery that will automagically make your Vimeo embeds work on an iPhone or iPad.

If you’re a Vimeo user you may have noticed that the service recently added mobile compatibility to their already-respectable list of features. There is a caveat, though. This new mobile compatibility does NOT extend to videos that you embed on your site – only videos that you watch on the Vimeo website. But what if you use Vimeo primarily for embedding videos on your own site, and you want those videos to be watchable on an iPhone or iPad? Vimeos official stance is… add a link. This is *not* ideal.

Fortunately, with a creative bit of jQuery and HTML, we can detect mobile browsers and automatically replace Vimeo’s flash player on a webpage with an HTML 5 one when appropriate. Hence, super-easy iPhone-friendly Vimeo embeds.

Update: Vimeo has recently updated their default video embed code to include automatic device detection, so this article is no longer relevant.

To use Vimeo’s new multi-device embeds, just log in to Vimeo.com and grab new embed code for whatever videos you want to update. If the idea of updating all your embeds makes you break out in a cold sweat, they also have a bit of JavaScript available that automatically “upgrades” your existing embeds – essentially performing the same function as the code here does. However, if you want to embed mobile-quality versions of your videos you will still need to have a Vimeo Plus account.

This article will remain available for archival purposes (you never know when something like this might come in handy).

> Vimeo Settings

This section covers Vimeo settings that must be used in order for mobile embedding to work.

> jQuery Magic

This section contains the jQuery code that does the video-switching magic.

2 Comments on (jQuery) Make Vimeo Embeds iPhone-friendly… Automagically!

  1. Posted 2010/08/05 at 11:12 am | Permalink

    First of all thanks for the code. This is exactly what I’m looking for. I’m developing a portfolio site for my company and am using embedded vimeo videos to make a gallery of video projects. I modified your code very slightly to act as a function that I can call once a video is loaded (because no vimeo videos are actually loaded at the beginning – they are created when the user clicks on a thumbnail). This worked (kind of). The little blue lego block that means “you can’t watch this video because it’s flash” is gone and has been replaced by a gray box with a quicktime logo in the bottom corner and the play button. However the play button is crossed out like a no smoking sign. Do you have any idea why this would be?

  2. Posted 2010/08/05 at 1:14 pm | Permalink

    Thanks for the positive feedback. It’s nice to know that people find some of this stuff useful.

    First, are you a Vimeo Plus member? I did a poor job of stressing this requirement in the above article (which I just updated to be clear on the issue), but you must be a Vimeo Plus member and you have to do these two things to your Vimeo settings for this work:

    a) Enable “Generate Mobile Versions” from your account settings under Video Preferences (the option appears for Plus users only), then wait for the mobile versions to be encoded (can take up to a couple hours in my experience)
    b) Set up your Video Privacy Settings to allow embedding

    If you’re not a Plus member and don’t want to subscribe, you might try removing or commenting-out the mobile-quality-check part of the JavaScript code (lines 24-26 in the example above). This would theoretically force mobile devices to load the larger, full-quality versions of the video. I haven’t tested this workaround, but I don’t see a technical reason why it wouldn’t work (other than the files potentially being too large for a phone to handle).

    Let me know if this helps.

Post a Comment

You must be logged in to post a comment.

RSS Twitter LinkedIn Facebook
Doing neato things with JavaScript, please wait...