On iPhone, Vimeo Javascript API .play() function doesn't work until the video has been played
Asked Answered
I

3

7

I'm using SwipeView (http://cubiq.org/swipeview) to create a swipeable slideshow on touchscreen devices. This is simple enough with images, but I want to include a Vimeo video in the slideshow as well. Unfortunately, because the iFrame captures your swipe input for itself (I believe this is what's happening at least), once you've arrived at the video player slide you can no longer swipe away from it unless you tap on the pagination. This isn't acceptable.

My solution was to hide the video somewhere on the page (using display: none; or height: 0; or whatever works) and use an image in the slideshow with a click event that triggers the video to play. Even if the video is hidden, when it plays it should go fullscreen and play (on iPhone, at least).

This technique is working fine when I test it on my desktop browser, but it's behaving strangely on the iPhone. For testing purposes, the video is being displayed underneath the slideshow. If I load the page and tap on the slide, it does nothing. However, if I play the video by tapping the actual video player, once the video has been closed I can play the video again by tapping the slide. Basically, once the video is played via the player I can play the video via the API but not without using the player once first.

Here is my code:

// Append the "hidden" video player to the page
$('.slider-container').append("<div class='mobile-video-slide'><iframe id='slideshow-player' src='//player.vimeo.com/video/81295681?title=0&byline=0&portrait=0&color=8bd4ee&api=1&player_id=slideshow-player' width='500' height='281' frameborder='0' webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe></div>");

// Get the player object
var iframe = $('#slideshow-player')[0],
    player = $f(iframe);

// When the video is ready
player.addEvent('ready', function() {
    // Add the click event to the slide
    $('#play-video').on('click', function() {
        // Play the video
        player.api('play');

        // Don't jump the page
        return false;
    });
});

I have determined through testing that the player.ready event is working properly, and that the image's click event is working properly. The problem seems to lie entirely in the player.api('play') call. Any help, or an alternate method of accomplishing this would be appreciated.

Intenerate answered 9/12, 2013 at 17:23 Comment(0)
I
9

After investigating this further and finding two other StackOverflow questions with the same issue (video players with js API and iPhones and video players with js API and iPhones) as well as the fact that this same behavior occurs on Vimeo's official API playground, it seems that either it's either a feature by design or a bug with the API.

Theorizing: Apple doesn't allow you to autoplay videos in mobile Safari. Perhaps this limitation is being imposed on Apple's end to prevent you from using the API to autoplay a video with Javascript.

Intenerate answered 10/12, 2013 at 15:19 Comment(4)
Yep, iOS does not allow videos to be played without a user interaction. For whatever reason, that interaction gets lost at some point through our API.Insomnolence
Hey @BradDougherty, I'm seeing identical behavior to what's described here in Chrome and Firefox for Android (4.4.2) as well, is this a known issue? No resolution for this on iOS yet either?Truong
Imagine going to a website with a movie on it, the download starts immediately because you assign it to auto-play. The user is overseas and their cell phone bill the next month is $25,000.Wilmott
If anyhow, we are allowed to access the play button object through jQuery, we can manually call the button.click(). But, due to CORS we can't access it. However I tried to test this on Vimeo playground website itself with the help of console, it was working like a charm.Insalivate
T
1

Safari remote console will show you the error on the console - Error: The viewer must initiate playback first.

If you seek a video before it's played, the video won't start (iOS only; other platforms seem ok).

player.addEvent('ready', function() {
    player.api("seekTo", 10);
});

So you need to do something like:

if ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g)) {
    player.addEvent('play', function(id) {
        player.api("seekTo", 10);
    });
} else {
    player.api("seekTo", 10);
}
Tonnage answered 15/6, 2015 at 3:47 Comment(1)
Vimeo staff updated me a few days ago and mentioned they had closed this one; seems to test out ok. My hack above is no longer needed.Tonnage
K
0

Some browser forbid to trigger the "play" via js in the beginning. There has to be real user interaction to play the video.

However, if you want to put the vimeo video in a slider what you can do is:

  1. apply a image with "play button" on top of the video
  2. set "opacity: 0" to the iframe and make it exactly the size&position of the "play button" (iframe have to be "allowfullscreen")
  3. once user trigger the video play for the first time. Make the iframe to normal size you want and let vimeo player.js take control.

In this case, user have to click the "play button" to trigger the video

Kliman answered 8/6, 2017 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.