Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Last active December 15, 2015 06:59
Show Gist options
  • Save Ahrengot/5220457 to your computer and use it in GitHub Desktop.
Save Ahrengot/5220457 to your computer and use it in GitHub Desktop.
SoundCloud based audio player
// Generated by CoffeeScript 1.6.1
(function() {
define(['soundcloudAPI'], function(SC) {
var AudioPlayer,
_this = this;
AudioPlayer = (function() {
function AudioPlayer(instruments, playerSelector) {
var _this = this;
this.instruments = instruments;
this.playerSelector = playerSelector != null ? playerSelector : '.instrument.has-track';
this.stop = function(instrument) {
return AudioPlayer.prototype.stop.apply(_this, arguments);
};
SC.initialize({
client_id: '*CLIENT ID*'
});
SC.whenStreamingReady(function() {
return _this.streamingReady = true;
});
this.muted = Modernizr.sessionstorage ? sessionStorage.getItem('muted') === 'true' : false;
$('body').toggleClass('muted', this.muted);
}
AudioPlayer.prototype.bindEvents = function() {
var _this = this;
$('body').on('mouseenter mouseleave', this.playerSelector, {
self: this
}, this.handleInstrumentInteraction);
$('body').on('click', '.mute', {
self: this
}, this.toggleMute);
$(document).on('ahrengot/stop_all_sounds', function() {
return _this.instruments.each(function(model) {
return model.set('playing', false);
});
});
return this.instruments.on('change:playing', this.toggleTrack, this);
};
AudioPlayer.prototype.toggleMute = function(e) {
var instrument, instrumentId, muted;
muted = e.data.self.muted = !e.data.self.muted;
if (Modernizr.sessionstorage) {
sessionStorage.setItem('muted', muted);
}
$('body').toggleClass('muted', this.muted);
instrumentId = $(this).data('instrument-id');
if (instrumentId && muted) {
return e.data.self.stopAllInstruments();
} else if (instrumentId && !muted) {
instrument = e.data.self.instruments.where({
'id': instrumentId
})[0];
if (instrument != null) {
return instrument.set('playing', true);
}
}
};
AudioPlayer.prototype.handleInstrumentInteraction = function(e) {
var instrument, self,
_this = this;
self = e.data.self;
if (self.muted) {
return;
}
if (e.data.self.streamingReady == null) {
return log("streaming not ready yet.");
}
switch (e.type) {
case 'mouseenter':
if (self.previewTimer != null) {
clearTimeout(self.previewTimer);
}
return self.previewTimer = setTimeout(function() {
var id;
id = $(_this).data('id');
return self.instruments.each(function(instrument) {
var isCurrent;
isCurrent = instrument.get('id') === id;
return instrument.set('playing', isCurrent);
});
}, 0);
case 'mouseleave':
instrument = self.instruments.where({
'id': $(this).data('id')
})[0];
if (instrument != null) {
return instrument.set('playing', false);
}
}
};
AudioPlayer.prototype.toggleTrack = function(instrument, playing) {
if (playing) {
return this.play(instrument);
} else {
return this.stop(instrument);
}
};
AudioPlayer.prototype.play = function(instrument) {
var trackId,
_this = this;
if (instrument.$el) {
instrument.$el.addClass('buffering');
}
trackId = instrument.get('track');
return SC.stream("/tracks/" + trackId, {
autoPlay: true,
useFlashBlock: true,
preferFlash: false,
useHighPerformance: true,
onload: function(success) {
if (!success) {
return log('Something went wrong trying to preview instrument.');
}
}
}, function(sound) {
return instrument.set('soundObj', sound);
});
};
AudioPlayer.prototype.stop = function(instrument) {
var soundObj;
soundObj = instrument.get('soundObj');
if (soundObj != null) {
return soundObj.unload();
}
};
AudioPlayer.prototype.stopAllInstruments = function() {
return this.instruments.each(function(m) {
return m.set('playing', false);
});
};
return AudioPlayer;
})();
return AudioPlayer;
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment