I am trying to load a vimeo player in an iOS app, and I need to be able to send it various commands (play, pause, seek) by javascript. I have a script written that will load and play a video that works in my browser, but when I load the same script into a UIWebView and run it, the video loads but does not play. I have no idea what's going wrong. I can get the behavior I want by loading a youtube video and sending it a play command, so I don't think it's apple disabling autoplay.
Script:
var playerExists = false;
var f, playerURL;
function loadPlayer(videoId) {
playerURL = 'http://player.vimeo.com/video/' + videoId + '?api=1&player_id=player';
//This changes the source of the iframe to the videoURL
if (playerExists) {
$('#player').attr('src', playerURL);
} else {
playerExists = true;
$('<iframe id="player"/>').appendTo('body');
$('#player').attr('src', playerURL);
$('#player').load(function () {
ready('player');
});
}
f = $('iframe');
}
// Helper function for sending a message to the player
function post(action, value) {
var data = {
method: action
};
if (value) {
data.value = value;
}
url = playerURL.split('?')[0];
f[0].contentWindow.postMessage(JSON.stringify(data), url);
}
function ready(player_id) {
playerReady = true;
post('play');
}
loadPlayer(66979809)
Here's a link to the working fiddle: http://jsfiddle.net/LunaEques/Hy43Z/
I finally figured it out. The key was in this line in the documentation: "You’ll need to be running on a web server instead of opening the file directly in your browser. Flash and JS security restrictions will prevent the API from working when run locally."
So, the problem was that when I was loading my javascript code, I used
NSURL* path = [[NSBundle mainBundle] URLForResource: @"vimeoPlayer"
withExtension: @"html"];
[self loadRequest: [NSURLRequest requestWithURL: path]];
This was loading the html locally, which disabled the javascript. Once I changed this to
NSString *path = [[NSBundle mainBundle] pathForResource:@"vimeoPlayer"
ofType:@"html"];
NSString *content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];
[self loadHTMLString:content baseURL:[NSURL URLWithString:@"http://player.vimeo.com/"]];
the javascript would run properly.