I have been attempting to use Vimeo's JavaScript API () in order to control an embedded video through my application, but I am encountering difficulties even with their provided example (http://jsfiddle.net/bdougherty/HfwWY/light/)
After downloading the jQuery and Froogaloop JavaScript files used in the fiddle, I copied them into Xcode and ensured they were placed under Bundle Resources, as suggested by some sources.
In my ViewController
, I have included:
NSString *path = [[NSBundle mainBundle] pathForResource:@"vimeo" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[_webby loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
The contents of my vimeo.html
file are as follows:
<html>
<body>
<iframe id="player1" src="http://player.vimeo.com/video/27855315?api=1&player_id=player1" width="400" height="225" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p>Video status: <span class="status">...</span></p>
<p><button>Play</button> <button>Pause</button></p>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="froogaloop2.min.js"></script>
<script type="text/javascript" src="player.js"></script>
</body>
Where jquery.min.js
can be found at https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js and froogaloop2.min.js
at
My player.js
is similar to the fiddle example with an ondomready function defined:
$(document).ready(function () {
var iframe = $('#player1')[0],
player = $f(iframe),
status = $('.status');
// When the player is ready, add listeners for pause, finish, and playProgress
player.addEvent('ready', function() {
status.text('ready');
player.addEvent('pause', onPause);
player.addEvent('finish', onFinish);
player.addEvent('playProgress', onPlayProgress);
});
// Call the API when a button is pressed
$('button').bind('click', function() {
player.api($(this).text().toLowerCase());
});
function onPause(id) {
status.text('paused');
}
function onFinish(id) {
status.text('finished');
}
function onPlayProgress(data, id) {
status.text(data.seconds + 's played');
}
});
The documentation mentions that: "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."
I am unsure how to avoid opening the JS file directly in my browser, although someone suggested changing this line in my ViewController
:
[_webby loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
I attempted modifying it to something like:
[_webby loadHTMLString:html baseURL:[NSURL URLWithString:@"http://player.vimeo.com/"]];
Unfortunately, this approach did not work for me either.