Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jwheare/www.playdar.org
Browse files Browse the repository at this point in the history
  • Loading branch information
jwheare committed Jul 19, 2009
2 parents 9d8121f + 70e86fb commit 0aedb9a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 48 deletions.
8 changes: 5 additions & 3 deletions demos/haudio.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ <h2>hAudio Demo</h2>
to represent tracks, but you could write a custom parser to find elements on any site
that represent artist/track names.
</p>
<form action="">
<input type="submit" value="Microformats Go!" id="gomicroformats" onclick="go_microformats()" disabled="disabled" />
</form>
<ul id="tracks">
<li>
<div class="haudio">
Expand Down Expand Up @@ -99,7 +101,7 @@ <h2>hAudio Demo</h2>
$(response.qid).className = 'playdar_found';

var sound = Playdar.player.register_stream(response.results[0]);
$(response.qid).innerHTML = "<a href=\"#\" title=\""+tt+"\" class=\"playbutton\">" + response.results.length + "<span style=\"display: none;\">" + response.results[0].sid + "</span></a>";
$(response.qid).innerHTML = "<a href=\"#\" title=\""+tt+"\" class=\"playbutton\">" + response.results.length + "<span style=\"display: none;\">" + response.results[0].sid + "<\/span><\/a>";
} else if (finalanswer) {
$(response.qid).className = 'playdar_notfound';
$(response.qid).innerHTML = ':(';
Expand All @@ -115,9 +117,9 @@ <h2>hAudio Demo</h2>
onStat: function (detected) {
var text;
if (detected) {
text = "<b style='color:green;'>Playdar ready</b>";
text = "<b style='color:green;'>Playdar ready<\/b>";
} else {
text = "<b style='color:red;'>Playdar unavailable</b><br/>You need Playdar, the music content resolver, installed and running. See <a href=\"http://<!--#echo var="domain" -->/\"><!--#echo var="domain" --></a>.";
text = "<b style='color:red;'>Playdar unavailable<\/b><br/>You need Playdar, the music content resolver, installed and running. See <a href=\"http://<!--#echo var="domain" -->/\"><!--#echo var="domain" --><\/a>.";
}
$('playdar_stat').update(text);
},
Expand Down
139 changes: 94 additions & 45 deletions static/playdar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Playdar = {
VERSION: "0.4.2",
VERSION: "0.4.3",
SERVER_ROOT: "localhost",
SERVER_PORT: "8888",
STATIC_HOST: "http://www.playdar.org",
Expand Down Expand Up @@ -28,6 +28,15 @@ Playdar = {
},
setup_player: function (soundmanager) {
new Playdar.Player(soundmanager);
},
unload: function () {
if (Playdar.player) {
// Stop the music
Playdar.player.stop_current(true);
} else if (Playdar.scrobbler) {
// Stop scrobbling
Playdar.scrobbler.stop();
}
}
};

Expand Down Expand Up @@ -158,10 +167,7 @@ Playdar.Client.prototype = {
}
},
clear_auth: function () {
// Stop the music
if (Playdar.player) {
Playdar.player.stop_all();
}
Playdar.unload();
// Revoke auth at the server
Playdar.Util.loadjs(this.get_revoke_url());
// Clear auth token
Expand All @@ -182,7 +188,8 @@ Playdar.Client.prototype = {
},
get_revoke_url: function () {
return this.get_base_url("/settings/auth/", {
revoke: this.auth_token
revoke: this.auth_token,
jsonp: 'Playdar.Util.null_callback'
});
},
get_auth_url: function () {
Expand Down Expand Up @@ -488,6 +495,7 @@ Playdar.Boffin = function () {
Playdar.Boffin.prototype = {
get_url: function (method, query_params) {
query_params = query_params || {};
query_params.call_id = new Date().getTime();
query_params.jsonp = query_params.jsonp || 'Playdar.Util.null_callback';
Playdar.client.add_auth_token(query_params);
return Playdar.client.get_base_url("/boffin/" + method, query_params);
Expand Down Expand Up @@ -529,6 +537,7 @@ Playdar.Scrobbler = function () {
Playdar.Scrobbler.prototype = {
get_url: function (method, query_params) {
query_params = query_params || {};
query_params.call_id = new Date().getTime();
query_params.jsonp = query_params.jsonp || 'Playdar.Util.null_callback';
Playdar.client.add_auth_token(query_params);
return Playdar.client.get_base_url("/audioscrobbler/" + method, query_params);
Expand Down Expand Up @@ -563,36 +572,36 @@ Playdar.Scrobbler.prototype = {
resume: function () {
Playdar.Util.loadjs(this.get_url("resume"));
},
get_sound_options: function (result, options) {
get_sound_callbacks: function (result) {
var scrobbler = this;
return {
onplay: function () {
scrobbler.start(result.artist, result.track, result.album, result.duration);
Playdar.Util.apply_property_function(options, 'onplay', this, arguments);
},
onbufferchange: function () {
if (this.isBuffering) {
scrobbler.pause();
} else {
scrobbler.resume();
onload: function () {
if (this.readyState == 2) { // failed/error
scrobbler.stop();
this.unload();
}
Playdar.Util.apply_property_function(options, 'onbufferchange', this, arguments);
},
onplay: function () {
// scrobbler.start isn't called until the first whileplaying callback
this.scrobbleStart = true;
},
onpause: function () {
scrobbler.pause();
Playdar.Util.apply_property_function(options, 'onpause', this, arguments);
},
onresume: function () {
onresume: function () {
scrobbler.resume();
Playdar.Util.apply_property_function(options, 'onresume', this, arguments);
},
onstop: function () {
scrobbler.stop();
Playdar.Util.apply_property_function(options, 'onstop', this, arguments);
},
onfinish: function () {
scrobbler.stop();
Playdar.Util.apply_property_function(options, 'onfinish', this, arguments);
if (!this.chained) {
scrobbler.stop();
}
},
whileplaying: function () {
// At this point we've finished the initial buffer and are actually playing
if (this.scrobbleStart) {
this.scrobbleStart = false;
scrobbler.start(result.artist, result.track, result.album, result.duration);
}
}
};
}
Expand All @@ -607,6 +616,9 @@ Playdar.Player = function (soundmanager) {
};
Playdar.Player.prototype = {
register_stream: function (result, options) {
if (this.streams[result.sid]) {
return false;
}
// Register result
this.streams[result.sid] = result;

Expand All @@ -615,20 +627,26 @@ Playdar.Player.prototype = {
url: Playdar.client.get_stream_url(result.sid)
}, options);

var callback_options = [options];
// Wrap sound progress callbacks with status bar
if (Playdar.status_bar) {
Playdar.Util.extend_object(sound_options, Playdar.status_bar.get_sound_options(result, options));
callback_options.push(Playdar.status_bar.get_sound_callbacks(result));
}
// Wrap sound lifecycle callbacks in scrobbling calls
if (Playdar.scrobbler) {
Playdar.Util.extend_object(sound_options, Playdar.scrobbler.get_sound_options(result, options));
callback_options.push(Playdar.scrobbler.get_sound_callbacks(result));
}
return this.soundmanager.createSound(sound_options);
Playdar.Util.extend_object(sound_options, Playdar.Util.merge_callback_options(callback_options));
var sound = this.soundmanager.createSound(sound_options);
if (sound_options.chained) {
sound.chained = sound_options.chained;
}
return sound;
},
play_stream: function (sid) {
var sound = this.soundmanager.getSoundById(sid);
if (this.nowplayingid != sid) {
this.stop_all();
this.stop_current();
if (sound.playState == 0) {
this.nowplayingid = sid;
// Update status bar
Expand All @@ -641,7 +659,12 @@ Playdar.Player.prototype = {
sound.togglePause();
return sound;
},
stop_all: function () {
stop_current: function (hard) {
if (hard) {
if (Playdar.scrobbler) {
Playdar.scrobbler.stop();
}
}
if (this.nowplayingid) {
var sound = this.soundmanager.getSoundById(this.nowplayingid);
sound.stop();
Expand All @@ -650,12 +673,12 @@ Playdar.Player.prototype = {
}
// Update status bar
if (Playdar.status_bar) {
Playdar.status_bar.stop_handler();
Playdar.status_bar.stop_current();
}
},
stop_stream: function (sid) {
if (sid && sid == this.nowplayingid) {
this.stop_all();
this.stop_current();
return true;
}
return false;
Expand Down Expand Up @@ -924,15 +947,13 @@ Playdar.StatusBar.prototype = {
this.show_resolution_status();
},

get_sound_options: function (result, options) {
get_sound_callbacks: function (result) {
return {
whileplaying: function () {
Playdar.status_bar.playing_handler(this);
Playdar.Util.apply_property_function(options, 'whileplaying', this, arguments);
},
whileloading: function () {
Playdar.status_bar.loading_handler(this);
Playdar.Util.apply_property_function(options, 'whileloading', this, arguments);
}
};
},
Expand Down Expand Up @@ -969,7 +990,7 @@ Playdar.StatusBar.prototype = {
var buffered = sound.bytesLoaded/sound.bytesTotal;
this.bufferhead.style.width = Math.round(buffered * this.progress_bar_width) + "px";
},
stop_handler: function () {
stop_current: function () {
this.playback.style.display = "none";
this.status.style.display = "";

Expand Down Expand Up @@ -1157,10 +1178,42 @@ Playdar.Util = {
return destination;
},

apply_property_function: function (obj, property, scope, args) {
if (obj && obj[property]) {
obj[property].apply(scope, args);
merge_callback_options: function (callback_options) {
var option_map = {};
var keys = [];
var i, options, option_name;
// Loop through an array of option objects
for (i = 0; i < callback_options.length; i++) {
options = callback_options[i];
// Process callback functions in each object
for (option_name in options) {
if (typeof (options[option_name]) == 'function') {
// Collect all matching option callbacks into one callback
if (!option_map[option_name]) {
keys.push(option_name);
option_map[option_name] = [];
}
option_map[option_name].push(options);
}
}
}
var final_options = {};
var key, mapped_options;
// Merge the mapped callback options
for (i = 0; i < keys.length; i++) {
var key = keys[i];
// Pass in the scope because closures don't really work
// with shared variables in a loop
final_options[key] = (function (key, mapped_options) {
return function () {
// Call each function that's been mapped to this property
for (var j = 0; j < mapped_options.length; j++) {
mapped_options[j][key].apply(this, arguments);
}
};
})(key, option_map[key]);
}
return final_options;
},

log: function (response) {
Expand All @@ -1171,11 +1224,7 @@ Playdar.Util = {
null_callback: function () {}
};

Playdar.Util.addEvent(window, 'unload', function () {
if (Playdar.scrobbler) {
Playdar.scrobbler.stop();
}
});
Playdar.Util.addEvent(window, 'unload', Playdar.unload);

/*!
* Sizzle CSS Selector Engine - v1.0
Expand Down

0 comments on commit 0aedb9a

Please sign in to comment.